Login or Register to Ask a Question and Join Our Community

Shell Scripting , need to search and print a line that contains a specific pattern

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Scripting , need to search and print a line that contains a specific pattern
# 1  
Old 08-31-2016
Shell Scripting , need to search and print a line that contains a specific pattern

Take example of below file.

nas1:/abc/test/test1                /test
nas1:/abc/test/test1/test2       /test/abc

Now i have a variable that contains "nas1:/abc/test/test1" value , so i need to search the above file for this variable and print only this line.

output expected :-
nas1:/abc/test/test1 /test

i tried cat abc.txt | grep -w "nas1:/abc/test/test1 " , its not working for few linux servers , so can i have some commands that can run on all distros.

Last edited by vgersh99; 08-31-2016 at 12:30 PM.. Reason: code tags, please!
# 2  
Old 08-31-2016
Hello mohit_vardhani,

You could use simple grepoption as follows.
grep "nas1:/abc/test/test1"  Input_file

But having said that, above code will give those lines also which have some more text after this line too like above code's output will be as follows.
nas1:/abc/test/test1                /test
nas1:/abc/test/test1/test2       /test/abc

You could use awk for same too.
awk -vVAL="nas1:/abc/test/test1" '($1==VAL)'  Input_file

Where variable's value is stored in above code as named VAL and it is looking for first field to match it, in case you need to change the string and change the matching field you could change above VAL=new_string_value and in place of $1you could use $2second field or $3 3rd field etc.

R. Singh
# 3  
Old 08-31-2016
Thanks , its working when running directly from terminal.

But when i am passing variable its not working.
I am using this under remote server

PR="\$( cat filename | awk -vVAL="$my_var" "($1==VAL)")"

Its giving syntax error , any suggestions.

Last edited by Scrutinizer; 08-31-2016 at 02:17 PM.. Reason: code tags
# 4  
Old 08-31-2016
Giving which syntax error? I'll try and guess.

You shouldn't need to put $( ) in quotes, and shouldn't escape the dollar sign.

Also, that's a useless use of cat. awk ... filename ought to do the same thing without the cat.
# 5  
Old 08-31-2016
Actually i am running this command remotely under the script , so it won;t run without escape the dollar sign

I have to grep this from df command , so i am using like below
PR="\$(df -hTP | awk -vVAL="$my_var" ($1==VAL))"

/bin/bash: command substitution: line 11: syntax error near unexpected token `('

Moderator's Comments:
Mod Comment Please use code tags even for so little, thanks

Last edited by vbe; 08-31-2016 at 02:13 PM..
# 6  
Old 08-31-2016
PR="$(df -hTP | awk -vVAL="$my_var" '$1==VAL')"

Last edited by vgersh99; 08-31-2016 at 03:16 PM..
# 7  
Old 09-01-2016
Originally Posted by mohit_vardhani
... its not working for few linux servers ...
What exactly is not working? How do the grep versions differ on the various system? How the input files (<TAB> / space separated?)?
Please note the -w wouldn't succeed, as / is NOT a word constituent character.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with print out record if first and next line follow specific pattern

Input file: pattern1 100 250 US pattern2 50 3050 UK pattern3 100 250 US pattern1 70 1050 UK pattern1 170 450 Mal pattern2 40 750 UK . . Desired Output file: pattern1 100 250 US pattern2 50 3050 UK pattern1 170 450 Mal pattern2... (3 Replies)
Discussion started by: cpp_beginner
3 Replies

2. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

3. 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

4. Programming

Print specific pattern line in c++

Discussion started by: cpp_beginner
10 Replies

5. Shell Programming and Scripting

Shell Scripting:Fetching content from each line with respect to pattern.

one.txt ONS.820.log:V 20Oct2010:GP ^ ^ ONS.123.log:V 21Oct2010:GP ^ ^ ONS.820.log:V 30Oct2010:GP ^ ^ want to make new file from existing one with addition. 20Oct2010 User KV001 has name tk003 with buffer- 338-1 21Oct2010 User KV003 has name tk002 with buffer- 338-2 30Oct2010 User KV002... (5 Replies)
Discussion started by: saluja.deepak
5 Replies

6. Shell Programming and Scripting

extract specific line if the search pattern is found

Hi, I need to extract <APPNUMBER> tag alone, if the <college> haas IIT Chennai value. college tag value will have spaces embedded. Those spaces should not be suppresses. My Source file <Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT ... (3 Replies)
Discussion started by: Sekar1
3 Replies

7. Shell Programming and Scripting

Search in specific position and print the whole line

I have two files abc.dat and sant.dat (Big file 60k rows) for every line's 1,4 of abc.dat need to seach if this is present on 28,4 of sant.dat every line. if its present the output needs to go to bde.dat Example: contents abc.dat aaaa bbbb cccc dddd contents sant.dat this is... (4 Replies)
Discussion started by: ssantoshss
4 Replies

8. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies

9. Shell Programming and Scripting

To print a specific line in Shell or awk.

Hi, I want to echo the 15th line from a file named as abc.txt, also i want to echo only the values in that line not the line number. Thanks in advance:) (4 Replies)
Discussion started by: tushar_tus
4 Replies

10. 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
Login or Register to Ask a Question

Featured Tech Videos