How to move files where the first line contains a string?

sed move line to top of file
awk
sed and awk pdf
sed and awk 101 hacks pdf
sed replace line in file
awk replace string in file
awk and sed together
grep awk '(print examples)

I am currently using the following command:

grep -l -Z -E '.*?FindMyRegex' /home/user/folder/*.csv | xargs -0 -I{} mv {} /home/destination/folder

This works fine. The problem is it uses grep on the entire file.

I would like to use the grep command on the FIRST line of the file only.

I have tried to use head -1 file | at the beginning, but it did not work.

A change I would add to your script is -

     for file in *.csv; do 
        head -1 "$file" | grep -l -Z -E '.*?FindMyRegex' | xargs -0 -I{} mv {} /home/destination/folder; 
     done

How to move files where the first line contains a string?, The problem is it uses grep on the entire file. I would like to use the grep command on the FIRST line of the file only. I have tried to use head -1 file | at the � The files that match the file type are opened and each line is read in loop. The find() method is called to check if the search string is present in a line. If found, the find method returns index where the string was found. The filename, line number, index and the whole line that contains the string is output to the user console. Program Source

you can maybe try sed '1q' file.csv | grep ... to search the regexp only in the first line.

How to move lines containing specific strings to top of file?, try with: <file gawk '/_005/,/_007/ { top[i++]=$0 next } { botom[j++]=$0 } END { for(k in top) print top[k] for(k in botom) print botom[k] }'. This will run a separate instance of sed to check each file, but at least it shouldn't read beyond the first line. It will fail syntactically if there are no hits. You might need -E depending on your regex.-n says don't print records from the files.-s says treat each file as a distinct input - this is so the filenames aren't always the first one.

You don't need grep or find, as long as your files don't have embedded newlines. I don't know an easy way off the top of my head to get sed to delimit with nulls.

mv $( for f in  /home/user/folder/*.csv;
      do sed -ns '1 { /yourPattern/F; q; }' $f;
      done ) /home/destination/folder/

EDIT

Rewrote with a loop. This will run a separate instance of sed to check each file, but at least it shouldn't read beyond the first line. It will fail syntactically if there are no hits.

You might need -E depending on your regex.

-n says don't print records from the files. -s says treat each file as a distinct input - this is so the filenames aren't always the first one.

This does require GNU sed for the F.

bash - How to list all files where the first line is a blank line, print FILENAME prints the file that matches. If the first line is not really empty but contains only whitespace, you can modify the regular expression� I've been using a lot of awk lately for csv files. But I've been using awk for csv files that contain 32 fields per line. For the first time, I've been given a csv file that contains one field per line (13 fields in each csv file).

[PDF] grep, awk and sed – three VERY useful command-line utilities Matt , Beginning at the first line in the file, grep copies a line into a following case, grep will print every line that does not contain the string "boo," and will display the where the 'for' loop uses a 'getline' command to move through the file, and only � If you have hundreds of lines in a text file and want to bulk delete lines that contain a word or string, this article is for you. Let's use the excellent third-party text editor Notepad++ (free) for deleting lines containing a word in a text-based file, using different methods.

using GNU awk to find the filenames, pipe the filenames into xargs

gawk -v pattern="myRegex" '
    FNR == 1 {if ($0 ~ pattern) printf "%s\0", FILENAME; nextfile}
' *.csv | xargs -0 echo mv -t destination

If it looks OK, remove "echo"

Handy one-liners for SED, Output file # should contain no more than one blank line between lines of text. sed N;s/\n=/ /;ta' -e 'P;D' # add commas to numeric strings, changing "1234567" to print first 10 lines of file (emulates behavior of "head") sed 10q # print first line� the /i switch of findstr makes the string to search not case-sensitive. The /m switch prints the file name, allowing you to set it as a variable (to move).----To find another string, change where i wrote "STRING" to what you want to search for. Also, change the "P:\a\t\h" to the path of your choice. Can I put a question here?

ed or red Command, The ed editor works on only one file at a time by copying it into a temporary edit Note The buffer contains the original file as well as editing information. in the String parameter and the new-line character if the first character of the String� The mv command is used to move and rename files and directories. For more information about the mv command, check the man page or type man mv in your terminal. New Linux users who are intimidated by the command line can use the GUI file manager to move their files. If you have any questions or feedback, feel free to leave a comment.

Basic vi Commands, [Alternate editors for UNIX environments include pico and emacs, a product of GNU.] The UNIX vi :0<Return> or 1G, move cursor to first line in file. :n<Return> or nG ?string, search backward for occurrence of string in text. n, move to next�

Findstr command examples and regular expressions, For example, to search for the string 'Windows' in the text file CLItips.txt, the If you want to print the lines having any of the given word set, you can enclose the list of @echo off for /F %%i in (pattern.txt) do ( echo Files containing %%i findstr /M I need to find the string “Milwaukee” in several *.txt files and then move the�

Comments
  • Using just head -1 file | ... instead of echo $(head ...) | ... is not only shorter and more efficient, but also safer. Right now you may mangle the first line due to missing quotes. Also I think grep -l won't work here as the input is coming from a pipe. You may want to use grep -q ... && mv "$file" ....
  • Hi thanks, I am getting 2 errors: 1 for file names with space in them. 2 is for when the grep matches: mv: cannot stat '(standard input)'...
  • mv: cannot stat 'standard input' no such file or directory (I moved the file part from the grep to sed)
  • if you are still using grep -l, then the output will be (standard input), which will not work with mv. The answer with awk given below is probably more adapted to your case.
  • the sed in your command will read all lines in each file. Even though it only does some logic on 1st line.
  • Yep. I had a 2q but it was quitting the entire process on the first file. One would wish it had a simple optimizer that recognized there was only one line to read and no output to generate, but I wouldn't assume. This is a case where awk kinda shines. :)
  • @Socowi what I meant .*?Pattern doesn't make sense. no matter it is greedy or not.
  • Ah, now I understand what you meant: grep looks for matches in the whole line, not only matches starting at the beginning of the line. Here we are not interested in the matching part, only in the yes/no answer, therefore .*pattern is the same as just pattern. And if the non-greedy regex .*?pattern was understood by grep -E, it would always match just pattern because there is nothing in front of .*?.
  • @Socowi yes, apart from if non-greedy is supported by -E or -P, the .*? or .* in his pattern doesn't make sense.
  • probably want to quote the filename in the printf statement output. quoting will get messy.
  • Hi, I specified the path for the csv before the wildcard. Got an error mv: missing file operand
  • Run just the gawk command without piping to xargs: are any filenames printed?