How to grep a word from a specific line till the end of file? [closed] - linux

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How to find a word for eg
Find "abc" starting from line 6 till the end of file ?

You can grep the output of tail command like this.
tail -n+6 file.txt | grep abc

In sed, it's easy to say "ignore lines in this address range" and then grep in the rest.
sed -n -e '1,5d' -e '/abc/p' file
The -n option says to not print all lines by default, we then simply delete lines 1 through 5, and then print any matching lines in the remainder.
There's also a block syntax, so you can say
sed -n '6,${/abc/p;}' file
but the precise syntax differs slightly between dialects (I think Linux would not demand a semicolon before the closing brace?) The address range 6,$ selects lines from the sixth through the end of the file.

One solution is to use awk, checking both the text and the line number (this script contains only the rule to select lines, it uses the default action which is to print the line):
awk 'NR >= 6 && /abc/' inputfile.txt
The following transcript shows this in action:
pax:~> cat inputfile.txt
1 abc
2 def
3 abc
4 xxx
5 yyy
6 abc
7 xyz
8 abc
9 abc
pax:~> awk 'NR >= 6 && /abc/' inputfile.txt
6 abc
8 abc
9 abc

Delete (d) lines from 1 to 5 (1,5); and also delete (the other d) lines not (!) containing abc (/abc/):
sed '1,5d;/abc/!d' input.txt

Related

Shell command to print the statements with N number of words present in other file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
Suppose I have a file with 3 lines:
output.txt:
Maruti
Zen
Suzuki
I used the command wc -l output.txt to get no. of lines
i got output as 3
Based on the above output I have to execute a command
echo CREATE FROM (sed -n 1p OUTPUT.txt)
echo CREATE FROM (sed -n 2p OUTPUT.txt)
echo CREATE FROM (sed -n 3p OUTPUT.txt)
:
:
echo CREATE FROM (sed -n np OUTPUT.txt)
Can you please suggest a command to replace 1 2 3 .....n in the above command based on the output i get (i.e., no. of lines in my file)
I just gave a sample explanation of my use case. Please suggest a command to execute n no. of times.
You just need one command.
sed 's/^/CREATE FROM /' output.txt
See also Counting lines or enumerating line numbers so I can loop over them - why is this an anti-pattern?

Find number 9 in file - linux terminal [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to find how many times number "9" appears in the result of the draw
(ignoring the ordinal number and date)
65. 11.05.1958 8,17,22,27,31,21
66. 18.05.1958 1,2,8,17,28,54
67. 25.05.1958 7,16,27,33,41,23
68. 01.06.1958 1,20,41,42,43,43
69. 08.06.1952 13,14,25,29,33,47
70. 15.06.1958 17,23,29,39,41,45
71. 22.06.1958 2,14,22,44,48,49
72. 29.06.1958 3,7,13,15,16,47
73. 06.07.1958 10,11,28,38,48,49
74. 13.07.1956 9,16,21,24,27,35
75. 20.07.1958 1,7,17,18,29,32
76. 27.07.1958 19,21,42,25,36,44
77. 03.08.1958 2,4,22,31,32,43
78. 10.08.1958 4,9,16,26,27,46
79. 17.08.1958 34,35,37,38,39,45
80. 24.08.1958 17,21,27,35,41,49
81. 31.08.1958 30,31,32,9,46,49
82. 07.09.1958 10,16,23,26,30,39
83. 14.09.1958 13,16,18,19,30,35
84. 21.09.1958 9,23,26,29,31,42
85. 28.09.1958 12,16,21,28,9,49
Use awk to remove the first 2 columns, then use grep -c to find the 9s. Put a \b around the regex to make sure it doesn't track 19,29,91,92,93,etc. Assuming your output is coming from FILENAME:
awk '{ print $3 }' FILENAME | grep -c '\b9\b
Assuming the text to search is in a file named output.txt:
cut <output.txt -d ' ' -f 3 | grep -w 9 | wc -l
The cut part splits by spaces, taking the third field.
grep -w finds 9 as a word, so the for example the line with 10,16,23,26,30,39 won't get picked.
Finally, wc -l counts how many lines we have.
And just for fun, here is yet another way, this one in Perl:
perl -nle '$c+=()=/\b(9)\b/g; END {print $c}' $FILENAME
This has the difference that it allows 9 to appear multiple times, and counts them all. Because it uses /g (global) and first casts the result to an array ()=, before adding the number of elements in the array to $c.

Command for printing part of a String? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a file name test,
it contains a String James Bond 007,
and i want to print only James Bond.
I tried the following commands:
$ strings -n 2 test
$ sed -n '/James/,/Bond/p' test
$ awk '{print substr($1,10)}' test
To print the first two words, you can use awk:
awk '{print $1, $2}' test
To print the first ten characters, you can put the file contents in a variable, then use the bash substring operation:
contents=$(cat test)
echo "${contents:0:10}"
Or in awk:
awk '{print substr($0, 1, 10)}' test
Notice that $0 means the whole line, and you have to give both a starting index and length to substr(). Indexes in awk start at 1 rather than 0.
In sed, /James/,/Bond/ is a line range expression; it processes all the lines starting from a line containing James until a line containing Bond. It doesn't process just part of the lines.

insert underscore in columns of a text file shell [duplicate]

This question already has answers here:
Replace whitespace with a comma in a text file in Linux
(9 answers)
Closed 6 years ago.
I have a tab separated text file f.txt like :
APPLE 10 5
BALL 20 6
CAT 30 7
I want the output to be
APPLE_10_5
BALL_20_6
CAT_30_7
I wrote the following to partially accomplish this, but I am stuck at the "paste" step. Can you help?
cat f.txt | cut -f 1,2,3 | paste ???
When the are sperated by one space using sed is a one liner.
sed -i "s/ /_/" input.txt

How can I use awk to display some certain fields in a text file? [duplicate]

This question already has answers here:
How to print third column to last column?
(19 answers)
Closed 9 years ago.
I have a text file like this:
1 http 2 3 4 5
2 dns 3 4
3 ftp 3 4 5 6 8
I want the output to be like this:
http 2 3 4 5
dns 3 4
ftp 3 4 5 6 8
Node that I just want to omit the first column in that file and the fields number in a certain line is not fixed.
Can I accomplish this goal using awk?
You can also use cut: cut -d' ' -f2-.
Edit: If you have to use awk, try awk '{$1=""; print $0}'
something like this maybe?
awk '{$1 =""; print }' file
If you don't care about the the field separators remaining in place, you can do this:
awk '{$1=""}1' filename
(Assuming filename is where you stored your data)
Drats, I was going to give you an Awk solution, then recommend cut. It looks like others have beaten me to the punch.
However, I see no sed solution yet!
$ sed -n `s/^[^ ][^ ]*//p` yourfile.txt
sed 's/^..//g' your_file
above should work based on the condition that the first field is always of a single character.
or in perl:
perl -pe 's/^[^\s]*\s//g' your_file

Resources