pattern search and print using sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pattern search and print using sh
# 1  
Old 12-14-2011
pattern search and print using sh

Hi All,

Have a file contains multiple line with the following

file.txt
Code:
insert: akdkas job:ksdjf
command: aldfasdf asdfsdfa asdfas.sh
machine: asdfa

need to grep for insert and machine and print only "akdkas,asdfa"
Code:
cat file.txt | egrep "insert|machine" | awk -F: '{print $2}'

output for the above
Code:
 akdkas job
 asdfa


but need to output as
Code:
akdkas,asdfa

Moderator's Comments:
Mod Comment How to use code tags

please advice, thank you so much in advance

Last edited by Franklin52; 12-15-2011 at 03:19 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 12-14-2011
First off, grep, fgrep and egrep can all read files, so there is no need to use cat. You couls use:
Code:
egrep "pat1|pat2" filename

which is more efficient than
Code:
cat filename | egrep "pat1|pat2"

Secondly, since you want bits from two different lines, best to let awk do all of the work. Something like this:

Code:
awk '
    /^insert:/ { save = $2; next }
    /^machine:/ { printf( "%s,%s\n", save, $2 ); next }
'  input-file


Last edited by agama; 12-14-2011 at 09:30 PM.. Reason: typo; example
This User Gave Thanks to agama For This Post:
# 3  
Old 12-14-2011
That is so cool Agama...

if i may want to make a bit complex

file.txt
insert: akdkas job:ksdjf
command: aldfasdf asdfsdfa asdfas.sh
machine: asdfa

how can i get the output for line "insert" and "command"

akdkas,asdfas.sh
# 4  
Old 12-14-2011
Same way:

Code:
awk '
    /^insert:/ { save = $2; next }
    /^command:/ { printf( "%s,%s\n", save, $3 ); next }
'  input-file

Just key on lines that start with command: and print the saved string from the insert line along with the third field.

If you want to print all three:
Code:
awk '
    /^insert:/ { save = $2; next }
    /^machine:/ { msave = $2; next }
    /^command:/ { printf( "%s,%s,%s\n", save, msave, $3 ); next }
'  input-file

# 5  
Old 12-14-2011
from the command line i need only to print "asdfas.sh" ..
# 6  
Old 12-14-2011
Then, simply this:

Code:
awk '/^command:/ {  $3; }'  input-file

This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

2. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

3. Shell Programming and Scripting

Print a Search Pattern after modification

Using either vim or awk or sed If I wish to to search for an unknown pattern - lets say 1B2495 or 1Q2345 so Search pattern : 1 and replace the 1 with 2 to print out : 2B2495 or 2Q2345 what are the possible commands. Struggling here - help would be appreciated. (5 Replies)
Discussion started by: dpooleco
5 Replies

4. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

5. Shell Programming and Scripting

Awk/Sed: Search Pattern from file and Print

Hi, I want to search for patterns (from a file) in a file and print the line matching the patterns and the line before it. I have to search for 100s of patterns from a file. Any help with AWK or Sed. Thanks! (2 Replies)
Discussion started by: saint2006
2 Replies

6. Shell Programming and Scripting

Search for Pattern and Print including Lines in between

Gurus, I have a big file that needs to be sorted out and I cant figure out what to do. The file name is as below: Name: xxxx yyyy nnnn Description: dfffgs sdgsgsf hsfhhs afgghhjdgj fjklllll gsfhfh Updated: jafgadsgg gsg Corrected: date today The file consists of line like these. ... (13 Replies)
Discussion started by: The One
13 Replies

7. UNIX for Dummies Questions & Answers

Print lines between the search pattern

hi, I have a file say x.txt containing xxx 123 bla bla ... you xxx dfk dbf ... me xxx ... ... keeps on.. i need to search for pattern in the line starting xxx in the file. If pattern matched, I need to fetch all the lines till i find next xxx. (17 Replies)
Discussion started by: prsshini
17 Replies

8. Shell Programming and Scripting

Print the line within the search pattern

Hi Guys, I had file as typedef struct { char TrailerType1; char TrailerTxt1; }Trailer; typedef struct { char PfigMoneyType; char PfigMoneyvalue; }PfigMoney; i need to print the lines within the search pattern. if i give the search pattern as... (3 Replies)
Discussion started by: manosubsulo
3 Replies

9. Shell Programming and Scripting

awk how to print if the search pattern contains speace

the data file is as below: > cat master.cnf /usr| location for usr|5 /src/ver1| version 1 |10 /src/ver2/log| ver 2 log |25 /src/sys/apps/log| Application log for sys|36 /src/sys/apps/conf| configuration location for app|45 /src/sys/apps/bin| binary location app|55my script is as below: ... (1 Reply)
Discussion started by: McLan
1 Replies
Login or Register to Ask a Question