The stream line editor 'sed' and the 'AWK' are the simple and highly useful languages for processing text files.
Here are some of the tips and tricks to use "sed". Later, I will post on AWK usage.
sed -n '/matchingWord/p' foo.txt
[Note that this is similar to: grep "matchingWord" foo.txt which seems to be very simple for this task. But for many instances, 'sed' may do more.]
Here are some of the tips and tricks to use "sed". Later, I will post on AWK usage.
sed -n '/matchingWord/p' foo.txt
[Note that this is similar to: grep "matchingWord" foo.txt which seems to be very simple for this task. But for many instances, 'sed' may do more.]
sed -i -e 's/match/replacingText/g' hello.txt
Here, the line containing 'match' will be replaced with 'replacingText.
-e : just displays the outcome but don't change the file
-i : change the file (writes in the file)
s : substitute
g : is for global substitution. If need only first match, remove g.
Last one is the file name.
You can use this to a bunch of files.
sed -i -e 's/match/replacingText/g' file*.txt
sed -i -e 's/match/replacingText/g' file*.txt
To delete lines with match pattern
sed -i '/pattern/d' FileName
To replace ":" with " "
The sed expression s/:/ /g replaces every ":" with a "space".
That is sed substitutes an empty space for every ":" character globally in the file
Sometimes, there may be a need to remove (which is better than replacing a line which contain special characters)
To remove use following onliner
For other posts like this, see
AWK
For more see following links:
https://stackoverflow.com/a/5410784/6628192
sed -i '/pattern/d' FileName
To replace ":" with " "
The sed expression s/:/ /g replaces every ":" with a "space".
That is sed substitutes an empty space for every ":" character globally in the file
Sometimes, there may be a need to remove (which is better than replacing a line which contain special characters)
To remove use following onliner
sed '/pattern to match/d' ./filename
To modify the file itself
sed -i '/pattern to match/d' ./filename
Replace the second line (or nth line, first line, third line, etc) using sed:
sed -i '2s/.*/0 5/' c*.in
For other posts like this, see
AWK
For more see following links:
https://stackoverflow.com/a/5410784/6628192
No comments:
Post a Comment