Grep to isolate a text file line and Awk to select a word?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep to isolate a text file line and Awk to select a word?
# 1  
Old 03-25-2012
Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it.

I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables.
Code:
$line = grep "1:" File | awk -F ":" '{print "Name: ", $1}' $line


Last edited by Franklin52; 03-26-2012 at 03:28 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-25-2012
Actually you don't need to use grep:

Code:
awk -F ":"  '/1:/ {print "Name: ", $1}' File

This prints the first field after "Name:". If there are more than one line with "1:" then it will print all of them.

I'm not sure what variable you needed to pass in; there wasn't really a question in your post.
# 3  
Old 03-25-2012
Thank you! If I wanted to store the awk output as a variable so that I can pass it into another awk statement.


Code:
var = $(awk -F ":"  '/1:/ {print "Name: ", $1}' File)

correct?

edit: syntax

Last edited by Franklin52; 03-26-2012 at 03:28 AM.. Reason: code tags
# 4  
Old 03-25-2012
No spaces before or after your equal sign:

Code:
var=$(awk -F ":" '/1:/ {print "Name: ", $1}' File)

Otherwise it looks good.
# 5  
Old 03-25-2012
Quote:
Originally Posted by agama
No spaces before or after your equal sign:

Code:
var=$(awk -F ":" '/1:/ {print "Name: ", $1}' File)

Otherwise it looks good.

Thank you very much, Smilie

---------- Post updated at 09:40 PM ---------- Previous update was at 08:55 PM ----------

I ran into a problem where if the text field contains the same variable in the text.

Code:
var2=$(awk -F ":" '/'$menu:'/ {print $4}' File1)
    echo $var2
    var3=$(awk -F ":" '/':$var2:'/ {print $3}' File2)
    echo $var3
    awk -F ":" '/':$var3:'/ {print "Managers Name: ", $1,$2,$3}' File3

I thought of cutting the text field first to the first 4 columns(all the is needed)

fileEdit=$(cut -d: -f1-f4 employee)
awk -F ":" '/':$var3:'/ {print "Managers Name: ", $1,$2,$3}' fileEdit

would that be the most logical or is there a more efficient proceeding?

Last edited by Ironguru; 03-25-2012 at 10:41 PM.. Reason: Bad habbit of using spaces
# 6  
Old 03-26-2012
I'm a bit confused. Is it possible to post a sample of the records in file 1 and 2 that show your problem? It might be possible to do this in a single awk, but without seeing the input that is being processed I cannot say. If there is sensitive data in the files (names, phone numbers etc.) then X them out so as not to make that data public.
# 7  
Old 03-26-2012
How about using awk to read all 3 files like this:

Code:
awk -F: -v menu=$menu 'FNR==1{F++}
   F==1&&$0~menu":"{v2=$4}
   F==2&&v2&&$0~":"v2":"{v3=$3}
   F==3&&v3&&$0~":"v3":"{print "Managers Name: ", $1,$2,$3}' File1 File2 File3


Last edited by Chubler_XL; 03-26-2012 at 01:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using grep to select specific patterns in text file?

How do I use grep to select words that start with I or O, end in box, and contain at least one letter in between them? the text file mailinfo.txt contains Inbox the Inbox Is a match box Doesn't match INBOX Outbox Outbox1 InbOX Ibox I box If the command works correctly it... (4 Replies)
Discussion started by: steezuschrist96
4 Replies

2. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

3. Shell Programming and Scripting

Select only line numbers using grep

Hai, I want to select only line numbers into a file if some pattern matches. I have written my script like below but its not working. #!/bin/sh file='/home/testfile1' filesearch='/home/test00' while read line do search=`echo $line |cut -c 1-24` echo $search echo `grep -n ""... (3 Replies)
Discussion started by: Subbu123
3 Replies

4. Shell Programming and Scripting

awk: isolate a part of a file name

hi there, i have a file named 'x20080613_x20100106.pwr1.gc', i want to isolate the part 'x20080613_x20100106' but by using the following line i isolate the part '.pwr1.gc': `awk '$0=substr($0, length($0)-7)' $temp` how can i reverse that? thank you! (3 Replies)
Discussion started by: friend
3 Replies

5. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

6. Shell Programming and Scripting

grep a line from a text file

Hi all, I need to grep a line from a log file which ensures me that the application server script is executed successfully. Some body please help me on this. I also need to write a while loop in which i need to use the status of the above grep output. Could some one please tell me how to use... (12 Replies)
Discussion started by: firestar
12 Replies

7. Shell Programming and Scripting

How to find and print the last word of each line from a text file

Can any one help us in finding the the last word of each line from a text file and print it. eg: 1st --> aaa bbbb cccc dddd eeee ffff ee 2nd --> aab ered er fdf ere ww ww f the o/p should be a below. ee f (1 Reply)
Discussion started by: naveen_sangam
1 Replies

8. Shell Programming and Scripting

[Bash]Attempting to Merge text from one file into another file at the line directly under a word

Hello, This is my first post on the forums. So I want to start by thanking anyone who is kind enough to read this post and offer advise. I hope to be an active contributor now that I've found these forums. I have an issue that I figure would be a good first post.. I have 2 text files... (5 Replies)
Discussion started by: efciem
5 Replies

9. Shell Programming and Scripting

Help need to cut the first word of a line in text file

Hi All, I would like help with a script which can get rid of the first work of all lines in text file. File 1 The name is Scott. Output : name is Scott ---------- Post updated at 02:38 PM ---------- Previous update was at 02:37 PM ---------- Hi ALL There is typo error in... (3 Replies)
Discussion started by: bubbly
3 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question