recursive change sed grep and sed - recursion
I'm still working in this little script but still having problems. Getting error
sed: no input files
I think the problem is on:
for i in `cat sed_output.txt` do sed 's/$oldstr/$newstr/g' > sed_output_new_old_string.txt done
echo "Enter string to be replaced"
read OLDSTRING
echo "Enter string to be placed"
read NEWSTRING
oldstr=$OLDSTRING #string to be replaced
newstr=$NEWSTRING #new string to be placed
echo "Enter folder path where we will find the string"
read FOLDERPATH
### grep oldstring and output it in grep_output.txt
grep -rl $oldstr $FOLDERPATH > grep_output.txt
### spaces or special characters on filenames, use sed to enclose them with quote
for i in `cat grep_output.txt`
do sed -e "s/'/'\\\\''/g;s/\(.*\)/'\1'/" grep_output.txt > sed_output.txt
done
for i in `cat sed_output.txt`
do sed 's/$oldstr/$newstr/g' > sed_output_new_old_string.txt
done
If
You know the file on which you need to do the substitution
You know what needs to be substituted
You know the replacement
then;
sed 's/substitution/replacement/g' filename
will scan file line by line looking for substitution text and replace it with replacement text.
For recursive substitution, you can do;
for file in path/to/folder; do
sed -i.bak 's/substitute/replacement/g' "$file"
done
The -i option will do in file changes. I added .bak to it so you have the backup of original files which will be renamed to file.bak.
If you are using variables in your sed then I would suggest you to use " instead of ' so that you variables gets interpolated correctly.
Related
Unix: Remove the filename, get only the extension and rename the extension using sed
I have lot filenames which have this kind format: 118-edorf.sum.fil 118-edorf.sum.fil_1 118-edorf.sum.fil_11 i want to remove 118-edorf.sum. from the filename and get only the extension , fil , fil_1 and fil_11 and rename it to asc, asc_1 and asc_11. So far, i can only remove 118-edorf.sum. using sed 's/.*\.//' The result will be fil fil_1 fil_11 So, how to rename it to asc asc_1 asc_11
To your solution only add additional substitution sed 's/^.*\.//; s/fil/asc/' To rename all files in directory using that criteria rename 's/^.*\.//; s/fil/asc/' * note: this rename command is untested
You can try this zsh foreach loop. Foreach can be somewhat slow for many files. You can also remove the echo statements for less noisier output. foreach C (`ls 118-edorf.sum.fil*`) f2=`echo $C|cut -d "." -f 3|cut -s -d "_" -f 2` if [ "${f2}" -eq "" ]; then echo "no underscore" mv $C asc elif echo "_${f2}" mv $C "asc_${f2}" end
This might work for you (GNU sed): sed -r 's/.*\.([^_]*(.*))/mv \1 asc\2/e' file Use an evaluated substitution command. The substitution removes everything upto the last . and retains everything thereafter as the first backreference. Everything following the first _ within that backreference is also retained as the second backreference. The RHS of the substitution command the forms a mv commmand using the parts from the LHS. If your sed does not have the e command/flag, use: sed 's/.*\.\([^_]*\(.*\)\)/mv \1 asc\2/' file | shell It might be safer to use: sed -r 's/.*\.(fil(.*))/mv \1 asc\2/e' file
sed command in bash input and output file
Does anyone know what this means? sed -e 's/\r$//' inputfile > outputfile This is what I have so far: \r refers to Carriage Return (CR) so possibly Swap the blanks for Return Carriage? in the inputfile? I'm not too sure really
It's changing files from CRLF-terminated lines into LF-terminated lines. The former tend to be Windows-type files where each line ends with a carriage-return/linefeed (CRLF or \r\n). UNIX-type files just have a newline character (LF or \n). Specifically, that sed command substitutes \r at the end of a line (indicated by $) with nothing, the same as s/xyzzy/plugh/ would change the first xyzzy in the line into plugh.
sed is the name of the program you call. -e tells sed that the following argument is the expression to run. s/\r$// is a substitution: it tells sed to replace carriage return at the end of line ($) with nothing. Sed does that for each line. inputfile is the file from which sed reads its input. > is a redirection operator, it means the output of sed will be redirected to outputfile. Basically, the result should be the same as dos2unix (sometimes renamed to fromdos).
redirecting in a shell script
I'm trying to write a script to swap out text in a file: sed s/foo/bar/g myFile.txt > myFile.txt.updated mv myFile.txt.updated myFile.txt I evoke the sed program, which swaps out text in myFile.txt and redirects the changed lines of text to a second file. mv then moves .updated txt file to myFile.txt, overwriting it. That command works in the shell. I wrote: #!/bin/sh #First, I set up some descriptive variables for the arguments initialString="$1" shift desiredChange="$1" shift document="$1" #Then, I evoke sed on these (more readable) parameters updatedDocument=`sed s/$initialString/$desiredChange/g $document` #I want to make sure that was done properly echo updated document is $updatedDocument #then I move the output in to the new text document mv $updatedDocument $document I get the error: mv: target `myFile.txt' is not a directory I understand that it thinks my new file's name is the first word of the string that was sed's output. I don't know how to correct that. I've been trying since 7am and every quotation, creating a temporary file to store the output in (disastrous results), IFS...everything so far gives me more and more unhelpful errors. I need to clear my head and I need your help. How can I fix this?
Maybe try echo $updatedDocument > $document
Change updatedDocument=`sed s/$initialString/$desiredChange/g $document` to updatedDocument=${document}.txt sed s/$initialString/$desiredChange/g $document Backticks will actually put the entire piped output of the sed command into your variable value. An even faster way would be to not use updatedDocument or mv at all by doing an in-place sed: sed -i s/$initialString/$desiredChange/g $document The -i flag tells sed to do the replacement in-place. This basically means creating a temp file for the output and replacing your original file with the temp file once it is done, pretty much exactly as you are doing.
#!/bin/sh #First, I set up some descriptive variables for the arguments echo "$1" | sed #translation of special regex char like . * \ / ? | read -r initialString echo "$2" | sed 's|[\&/]|\\&|g' | read -r desiredChange document="$3" #Then, I evoke sed sed "s/${initialString}/${desiredChange}/g" ${document} | tee ${document} don't forget that initialString and desiredChange are pattern interpreted as regex, so a trnaslation is certainly needed sed #translation of special regex char like . * \ / ? is to replace by the correct sed (discuss on several post on the site)
How to remove blank lines from a Unix file
I need to remove all the blank lines from an input file and write into an output file. Here is my data as below. 11216,33,1032747,64310,1,0,0,1.878,0,0,0,1,1,1.087,5,1,1,18-JAN-13,000603221321 11216,33,1033196,31300,1,0,0,1.5391,0,0,0,1,1,1.054,5,1,1,18-JAN-13,059762153003 11216,33,1033246,31300,1,0,0,1.5391,0,0,0,1,1,1.054,5,1,1,18-JAN-13,000603211032 11216,33,1033280,31118,1,0,0,1.5513,0,0,0,1,1,1.115,5,1,1,18-JAN-13,055111034001 11216,33,1033287,31118,1,0,0,1.5513,0,0,0,1,1,1.115,5,1,1,18-JAN-13,000378689701 11216,33,1033358,31118,1,0,0,1.5513,0,0,0,1,1,1.115,5,1,1,18-JAN-13,000093737301 11216,33,1035476,37340,1,0,0,1.7046,0,0,0,1,1,1.123,5,1,1,18-JAN-13,045802041926 11216,33,1035476,37340,1,0,0,1.7046,0,0,0,1,1,1.123,5,1,1,18-JAN-13,045802041954 11216,33,1035476,37340,1,0,0,1.7046,0,0,0,1,1,1.123,5,1,1,18-JAN-13,045802049326 11216,33,1035476,37340,1,0,0,1.7046,0,0,0,1,1,1.123,5,1,1,18-JAN-13,045802049383 11216,33,1036985,15151,1,0,0,1.4436,0,0,0,1,1,1.065,5,1,1,18-JAN-13,000093415580 11216,33,1037003,15151,1,0,0,1.4436,0,0,0,1,1,1.065,5,1,1,18-JAN-13,000781202001 11216,33,1037003,15151,1,0,0,1.4436,0,0,0,1,1,1.065,5,1,1,18-JAN-13,000781261305 11216,33,1037003,15151,1,0,0,1.4436,0,0,0,1,1,1.065,5,1,1,18-JAN-13,000781603955 11216,33,1037003,15151,1,0,0,1.4436,0,0,0,1,1,1.065,5,1,1,18-JAN-13,000781615746
sed -i '/^$/d' foo This tells sed to delete every line matching the regex ^$ i.e. every empty line. The -i flag edits the file in-place, if your sed doesn't support that you can write the output to a temporary file and replace the original: sed '/^$/d' foo > foo.tmp mv foo.tmp foo If you also want to remove lines consisting only of whitespace (not just empty lines) then use: sed -i '/^[[:space:]]*$/d' foo Edit: also remove whitespace at the end of lines, because apparently you've decided you need that too: sed -i '/^[[:space:]]*$/d;s/[[:space:]]*$//' foo
awk 'NF' filename awk 'NF > 0' filename sed -i '/^$/d' filename awk '!/^$/' filename awk '/./' filename The NF also removes lines containing only blanks or tabs, the regex /^$/ does not.
Use grep to match any line that has nothing between the start anchor (^) and the end anchor ($): grep -v '^$' infile.txt > outfile.txt If you want to remove lines with only whitespace, you can still use grep. I am using Perl regular expressions in this example, but here are other ways: grep -P -v '^\s*$' infile.txt > outfile.txt or, without Perl regular expressions: grep -v '^[[:space:]]*$' infile.txt > outfile.txt
sed -e '/^ *$/d' input > output Deletes all lines which consist only of blanks (or is completely empty). You can change the blank to [ \t] where the \t is a representation for tab. Whether your shell or your sed will do the expansion varies, but you can probably type the tab character directly. And if you're using GNU or BSD sed, you can do the edit in-place, if that's what you want, with the -i option. If I execute the above command still I have blank lines in my output file. What could be the reason? There could be several reasons. It might be that you don't have blank lines but you have lots of spaces at the end of a line so it looks like you have blank lines when you cat the file to the screen. If that's the problem, then: sed -e 's/ *$//' -e '/^ *$/d' input > output The new regex removes repeated blanks at the end of the line; see previous discussion for blanks or tabs. Another possibility is that your data file came from Windows and has CRLF line endings. Unix sees the carriage return at the end of the line; it isn't a blank, so the line is not removed. There are multiple ways to deal with that. A reliable one is tr to delete (-d) character code octal 15, aka control-M or \r or carriage return: tr -d '\015' < input | sed -e 's/ *$//' -e '/^ *$/d' > output If neither of those works, then you need to show a hex dump or octal dump (od -c) of the first two lines of the file, so we can see what we're up against: head -n 2 input | od -c Judging from the comments that sed -i does not work for you, you are not working on Linux or Mac OS X or BSD — which platform are you working on? (AIX, Solaris, HP-UX spring to mind as relatively plausible possibilities, but there are plenty of other less plausible ones too.) You can try the POSIX named character classes such as sed -e '/^[[:space:]]*$/d'; it will probably work, but is not guaranteed. You can try it with: echo "Hello World" | sed 's/[[:space:]][[:space:]]*/ /' If it works, there'll be three spaces between the 'Hello' and the 'World'. If not, you'll probably get an error from sed. That might save you grief over getting tabs typed on the command line.
grep . file grep looks at your file line-by-line; the dot . matches anything except a newline character. The output from grep is therefore all the lines that consist of something other than a single newline.
with awk awk 'NF > 0' filename
To be thorough and remove lines even if they include spaces or tabs something like this in perl will do it: cat file.txt | perl -lane "print if /\S/" Of course there are the awk and sed equivalents. Best not to assume the lines are totally blank as ^$ would do. Cheers
You can sed's -i option to edit in-place without using temporary file: sed -i '/^$/d' file
Replace \n with \r\n in Unix file
I'm trying to do the opposite of this question, replacing Unix line endings with Windows line endings, so that I can use SQL Server bcp over samba to import the file. I have sed installed but not dos2unix. I tried reversing the examples but to no avail. Here's the command I'm using. sed -e 's/\n/\r\n/g' myfile I executed this and then ran od -c myfile, expecting to see \r\n where there used to be \n. But there all still \n. (Or at least they appear to be. The output of od overflows my screen buffer, so I don't get to see the beginning of the file). I haven't been able to figure out what I'm doing wrong. Any suggestions?
When faced with this, I use a simple perl one-liner: perl -pi -e 's/\n/\r\n/' filename because sed behavior varies, and I know this works.
What is the problem with getting dos2unix onto the machine? What is the platform you are working with? Do you have GNU sed or regular non-GNU sed? On Solaris, /usr/bin/sed requires: sed 's/$/^M/' where I entered the '^M' by typing controlV controlM. The '$' matches at the end of the line, and replaces the end of line with the control-M. You can script that, too. Mechanisms expecting sed to expand '\r' or '\\r' to control-M are going to be platform-specific, at best.
You don't need the -e option. $ matches the endline character. This sed command will insert a \r character before the end of line: sed 's/$/\r/' myfile
Just adding a \r (aka ^M, see Jonathan Leffler's answer) in front of \n is not safe because the file might have mixed mode EOL, so then you risk ending up with some lines becomming \r\r\n. The safe thing to do is first remove all '\r' characters, and then insert (a single) \r before \n. #!/bin/sh sed 's/^M//g' ${1+"$#"} | sed 's/$/^M/' Updated to use ^M.
sed 's/\([^^M]\)$/\0^M/' your_file This makes sure you only insert a \r when there is no \r before \n. This worked for me.
Try using: echo " this is output" > input sed 's/$/\r/g' input |od -c
Maybe if you try it this way cat myfile | sed 's/\n/\r\n/g' > myfile.win will work, from my understanding your just making the replacements to the console output, you need to redirect output to a file, in this case myfile.win, then you could just rename it to whatever you want. The whole script would be (running inside a directory full of this kind of files): #!/bin/bash for file in $(find . -type f -name '*') do cat $file | sed 's/\n/\r\n/g' > $file.new mv -f $file.new $file done