!!EMERGENCY!! - GREP/CUT to array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting !!EMERGENCY!! - GREP/CUT to array
# 1  
Old 08-25-2010
!!EMERGENCY!! - GREP/CUT to array

hi people,

I have a text file containing data, seperated by TAB. I want to process this tab'ed data as variable. how can I assign this?

Ex:
Code:
11aaa 12000 13aaa 14aaa 15aaa 16aaa 17aaa 
21aaa 22000 23aaa 24aaa 25aaa 26aaa 27aaa
31aaa 32000 33aaa 34aaa 35aaa 36aaa 37aaa
41aaa 42000 43aaa 44aaa 45aaa 46aaa 47aaa
51aaa 52000 53aaa 54aaa 55aaa 56aaa 57aaa
61aaa 62000 63aaa 64aaa 65aaa 66aaa 67aaa
71aaa 72000 73aaa 74aaa 75aaa 76aaa 77aaa

I want to fetch, lets say, "54aaa" (5th row, 4th col) as a variable to be able to process it. then I will call it as $qweasd=nameofthisvaribale. I have tried with grep and cut but no result as I want.

thanks for your assist.

Last edited by radoulov; 08-25-2010 at 05:22 AM.. Reason: Added code tags!
# 2  
Old 08-25-2010
Try:
Code:
awk 'NR==5{print $4}' infile

You can assign it to variable qweasd like this:
Code:
qweasd=$(awk 'NR==5{print $4}' infile)

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-25-2010
Where's the fire?

Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.


Thank you.

The UNIX and Linux Forums
# 4  
Old 08-25-2010
absolutely excellent Scrutinizer thank you very much. this is absolutely what I want!!

thanks for your reply & timing Smilie)

---------- Post updated at 11:34 AM ---------- Previous update was at 11:26 AM ----------

Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk 'NR==5{print $4}' infile

You can assign it to variable qweasd like this:
Code:
qweasd=$(awk 'NR==5{print $4}' infile)



---------- Post updated at 11:37 AM ---------- Previous update was at 11:34 AM ----------

but I wonder, what about if my data is seperated by SPACE, or DOT, or COMMA? which parameter in this code specifies the TAB filter? can you give example for, lets say, SPACE?
# 5  
Old 08-25-2010
By default awk separates using space characters so it will handle to TABs as well as space. To make it e.g. use TABs only you can use:
Code:
awk -F "\t" 'NR==5{print $4}' infile

likewise for . or ,
Code:
awk -F. 'NR==5{print $4}' infile
awk -F, 'NR==5{print $4}' infile

To use any number of space characters only (and not TABs):
Code:
awk -F" *" NR==5{print $4}' infile

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 08-25-2010
Quote:
Originally Posted by Scrutinizer
By default awk separates using space characters so it will handle to TABs as well as space. To make it e.g. use TABs only you can use:
Code:
awk -F "\t" 'NR==5{print $4}' infile

likewise for . or ,
Code:
awk -F. 'NR==5{print $4}' infile
awk -F, 'NR==5{print $4}' infile

To use any number of space characters only (and not TABs):
Code:
awk -F" *" NR==5{print $4}' infile

thank you very much Sucritinizer. that's all I want. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Emergency grep/delete trouble!!!

Hi everyone, thank you so much for reading. I built a user reference page from /etc/password for a class project. Now I need two shell scripts: 1) Add names to the reference page 2) Delete names from the reference page. I know grep -v is involved somehow and some piping but I am super stuck. ... (1 Reply)
Discussion started by: jsmpdx
1 Replies

2. Shell Programming and Scripting

Cut Lines using grep

Hi, I have a SQL script with "create table" and "alter table" statements and I want to cut all the alter table statements from original file (A) and move it to a different file (B). Can you please me the option. Thanks a lot for your time. (3 Replies)
Discussion started by: bhupinder08
3 Replies

3. Shell Programming and Scripting

grep with cut option??

This is my command-line code in my script, passwd=`grep $passwd $userfile | cut -f2 -d: login_users > retrieve` the cut -f2 -d: login_users > retrieve searches and prints the whole column two. what I need is one of the items in the column two only.. what option can I add to my cut... (2 Replies)
Discussion started by: jenimesh19
2 Replies

4. Shell Programming and Scripting

Cut text and assign them into array

file.txt : is delimiter: abc:def:ghi jkl:mno: pqr 123:456:789 if I do the cut command, and cut the first column, and echo it out I will get the output: abc jkl 123 How can I assign the column of text that I've cut into Array? e.g If I were to echo array array it will output as:... (9 Replies)
Discussion started by: andylbh
9 Replies

5. Slackware

How should I cut this line using cut and grep?

not sure how to do it. wan't to delete it using cut and grep ince i would use it in the shell. but how must the command be? grep "64.233.181.103 wwwGoogle.com" /etc/hosts | cut -d the delimeter is just a space. can you help meplease. :D (1 Reply)
Discussion started by: garfish
1 Replies

6. Shell Programming and Scripting

Using grep and cut within awk

Hi My input file looks like as follows: say a.txt "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss... (5 Replies)
Discussion started by: bittoo
5 Replies

7. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

tail, grep and cut

Hello all, I have some weird problem that kinda baffles me. Say I have the following test file: claudia:~/tmp$ cat testfile.txt This is a test line This is the second test line And yeah, this is the third test line Then say I want to tail the file, grep for the word "third" then... (7 Replies)
Discussion started by: sylaan
7 Replies

9. UNIX for Dummies Questions & Answers

to assign cut values to an array

i need to seperate values seperated by delimiters and assign it to an array.. can u plz help me on that. Variables = "asd,rgbh,(,rty,got,),sroe,9034," i need to assign the variables into arrays.. like.. var=asd var=rgbh.. and so on how do i do this. i need to reuse the values stored in... (6 Replies)
Discussion started by: Syms
6 Replies

10. Shell Programming and Scripting

grep and cut....

hi, i have a simple question: in hpux 11i; this cmd : cat mailtest |grep sekar >test1 gives the output file with the name test1. i want to remove the line which contains the "sekar" and put the result in the new file.... what is the command for that?.. (2 Replies)
Discussion started by: sekar sundaram
2 Replies
Login or Register to Ask a Question