let say I have a text file and I want to show echo from it only the 3rd word in the 48th line, how can I do it?
I was able to show a specific line from the text file:
sed -n 48p log.txt
but now I need to find a way to show only the 3rd word...
You can try this:
sed -n 48p log.txt | cut -d' ' -f3
Gets convoluted to do in only sed:
sed -En '48{s/(\s*\S+\s*){2}//;s/\s+.*//;p}'
Delete the first two words, then delete everything except the remaining first word, then print.
Related
I'm trying to learn sed but getting stuck when trying to replace first word wih the 3rd. I was thinking about the above code, but it doesn't work.
Also, is there any way of splitting the line if the words are separated by ":" using sed?
sed "s/\(^[a-z,0-9]*\) \(.*\) \([a-z,0-9]*\)/\1 \2 \1/"
From your comment below it sounds like you actually want to replace the third word with the first one rather than the other way around. If so then:
$ echo 'first:second:third' | sed 's/\(\([^:]*\).*:\).*/\1\2/'
first:second:first
or if you have many fields to manipulate:
$ echo 'first:second:third' | sed 's/\([^:]*\):\([^:]*\):\([^:]*\)/\1:\2:\1/'
first:second:first
but you should really use awk for anything involving fields anyway:
$ echo 'first:second:third' | awk 'BEGIN{FS=OFS=":"} {$3=$1} 1'
first:second:first
I'm very much a junior when it comes to the sed command, and my Bruce Barnett guide sits right next to me, but one thing has been troubling me. With a file, can you filter it using sed to select only specific items? For example, in the following file:
alpha|november
bravo|october
charlie|papa
alpha|quebec
bravo|romeo
charlie|sahara
Would it be possible to set a command to return only the bravos, like:
bravo|october
bravo|romeo
With sed:
sed '/^bravo|/!d' filename
Alternatively, with grep (because it's sort of made for this stuff):
grep '^bravo|' filename
or with awk, which works nicely for tabular data,
awk -F '|' '$1 == "bravo"' filename
The first two use a regular expression, selecting those lines that match it. In ^bravo|, ^ matches the beginning of the line and bravo| the literal string bravo|, so this selects all lines that begin with bravo|.
The awk way splits the line across the field separator | and selects those lines whose first field is bravo.
You could also use a regex with awk:
awk '/^bravo|/' filename
...but I don't think this plays to awk's strengths in this case.
Another solution with sed:
sed -n '/^bravo|/p' filename
-n option => no printing by default.
If line begins with bravo|, print it (p)
2 way (at least) with sed
removing unwanted line
sed '/^bravo\|/ !d' YourFile
Printing only wanted lines
sed -n '/^bravo\|/ p' YourFile
if no other constraint or action occur, both are the same and a grep is better.
If there will be some action after, it could change the performance where a d cycle directly to the next line and a p will print then continue the following action.
Note the escape of pipe is needed for GNU sed, not on posix version
I know a line number in a file, wherein I want to keep the first word and delete the rest till the end of the line. How do I do this using sed ?
So lets say, I want to go to line no 10 in a file, which looks like this -
goodword "blah blah"\
and what i want is
goodword
I have tried this - sed 's/([a-z])./\1/'
But this does it on all the lines in a file. I want it only on one specified line.
If by "first word" you mean "everything up to the first space", and if by "retain this change in the file itself" you mean that you don't mind creating a new file with the same name as the previous file, and if you have a sed that supports -i, you can probably just do:
sed -i '10s/ .*//' input-file
If you want to be more restrictive in the definition of a word, you can use '10s/\([a-z]*\).*/\1/'
Can you use grep or awk to grab just one line, and then pipe it into sed (if grep or awk couldn't do the entire job for you) to work on just one line? I think the key here is isolating that one line first, and then worrying about extracting something from it.
Using awk
awk 'NR==10 {print $1}' file
goodword
grep shows the lines where the search word is found. I have a text file where there is no line break and entire text is on a single line. Is there any way to instruct grep to show the contents of the left and right (just like -after, -before)?
I will like to see the entire sentence. The words between 2 fullstops. (i.e. the sentence where the word is found)
Use awk with the period as record separator and filter the records on the required pattern:
awk -v RS="." '/pattern/' file
Which is shorthand for:
awk -v RS="." '/pattern/{print}' file
You can temporarily chop the text into lines:
cat text.txt | sed 's/\./.\n/g' | grep pattern
Try this:
grep -o '[^.]*word[^.]*\.' file
Greetings!
I have been tasked to create a report off files we receive from our hardware suppliers. I need to grep these files for two fields 'Test_Version' and 'Model-Manufacturer' ; for each field, I need to capture their corresponding values.
In a previous post, I found help to create a basic report like so:
find . -name "*.VER" -exec egrep -A 1 'Test_Version=|Model-Manufacturer:' {} ';'
Model-Manufacturer:^M
R22-100^M
Test_Version=2.6.3^M
Model-Manufacturer:^M
R16-300^M
Test_Version=2.6.3^M
However, the data that's output is riddled with DOS carriage returns "^M". My boss wants "Model-Manufacturer" to show like "Test_Version" i.e
Model-Manufacturer:R22-100
Test_Version=2.6.3
Model-Manufacturer:R16-300
Test_Version=2.6.3
Using sed, I attempted to remove the "^M" characters for "Model-Manufacturer" but to no avail:
find . -name "*.VER" -exec egrep -A 1 'Test_Version=|Model-Manufacturer:' {} ';' | sed 's/Model-Manufacturer:^M//g'
This command has not effect. What am I missing here?
Give this a try:
sed '/Model-Manufacturer:/s/\r//g'
If you also have newlines and you want to combine the two lines into one, you can use one of the techniques shown in the answers to your previous question.
you can remove the carriage returns using dos2unix if you have it. Or using tr
tr -d '\r' < file
If you're using Bash as your shell, or creating the script in vi, you should be able to do:
sed -e 's/<Ctrl-V><Ctrl-M>//g'
to remove the CRs.
Ctrl-V (the keystroke on your keyboard) inserts the next keystroke literally, and Ctrl-M is carriage return.