Linux using grep command [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
After using grep command, i will give you a small example
grep test
test this is first line : 1
test this is second line : 2
test this is third line : 3
test this is fourth line : 4
How to filter the last line after grep command executed
finally i need the result 4

If I don't misunderstand you:
$grep test | tail -1
>test this is fourth line : 4
$grep test | tail -1 | awk '{print $7}'
>4
$7 is 'the seventh column' in awk.

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?

Extracting month from day using 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 am having a text file containing a list of date and time just like the sample below -
posted_at"
2012-06-09 11:48:31"
2012-08-09 12:40:02"
2012-04-09 13:10:00"
2012-03-09 13:40:00"
2012-10-09 14:30:01"
2012-12-09 15:30:00"
2012-11-09 16:20:00"
I want to extract the month from each line.
P.S - grep should not be used at any point of the code
Thanks in advance!
First select date pattern :
egrep '[0-9]{4}-[0-9]{2}-[0-9]{2} ' content_file
Second, extract the month :
awk -F '-' '{print $2}'
Third redirect to desired file :
>> desired_file
So mix of all this with | to final solution :
egrep '[0-9]{4}-[0-9]{2}-[0-9]{2} ' content_file| awk -F '-' '{print $2}'>> desired_file
VoilĂ 

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.

Playing Sound in Putty during tail Option [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
My requirement is to play an alert (Play Sound) in putty if any Exception occurs in the Application , so for this purpose i have tried the below way
tail -f flex.log | grep "Exception" --color paplay alert.wav
But even though the word Exception occurs in flex Log File during tail , but it is not playing the sound .
Please let me know if there is any mistake in the above command .
I am using centOS 8 as OS and script is bash .
This will find all words with Exception and replace it with the bell character, your terminal should beep/flash/whatever you've set up to happen during a terminal bell.
tail -f flex.log | grep "Exception" | sed -e $'s/Exception/Exception\a/'
To see ALL lines of flex.log but bell only on "Exception":
tail -f flex.log | sed -e $'s/Exception/Exception\a/'

Unix/Linux comma deliminated files [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have comma deliminated file that basically has the structure of:
1, 2, 3, 4, 5 ,,,, 6, etc
I have to count the number of unique 6th columns. Pleasseee help
(btw this is an intro to unix/linux class so this should be able to be done with basic commands)
cut -d "," -f 6 myFile |sort |uniq -c |wc -l
Looking into my crystal ball, I see that your class is discussing awk. Try
awk -F, '!a[$6]++{c++} END{ print c }' input-file

Resources