[ask]awk in csh to extract content from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [ask]awk in csh to extract content from file
# 1  
Old 07-27-2011
[ask]awk in csh to extract content from file

Please suggest a method (in c shell or any other shell) to implement following:

-To read file1.txt (sample file1 given below)
-To save name field in a variable <name>
-To save parameter field in a variable <parameter>
for ex. let a line in file1.txt be :

Code:
bill height weight

the extracted results can be checked as:
echo "$bill" # prints - bill
echo "$height" #prints - height
echo "$weight" #prints - weight

sample file1.txt

Code:
ani year month
sam time day
bill height weight
.
.

The idea is to enter the data in file1 as and when requiring and the script automatically extracting even the new line.
I guess it can be possible using "awk", but need HELP!

---------- Post updated at 02:44 PM ---------- Previous update was at 01:38 PM ----------

--- added later-------
or is it possible to do what i have asked in previous post?
please advice Smilie
# 2  
Old 07-27-2011
Code:
 
awk '{ a=$1;b=$2;c=$3 } END { print a , b , c}' input_file

Which ensure you wil get only the "last" line fields which i believe is the last added line.
# 3  
Old 07-27-2011
@panyam, thanks for the answer, but its not exactly what i am looking for.

what i want from all of it is to write a script to extract one element of each filed at a time and then save them in a variable of name same as the element extracted.

It is confusing, please check the example in my first post.

Thanks
# 4  
Old 07-27-2011
Code:
bash-3.00$ cat test
abc efg
hij
klm
 
bash-3.00$ awk '{for(i=1;i<=NF;i++)printf("%s=%s\n",$i,$i)}' test > test.var && . test.var
 
bash-3.00$ echo $abc
abc
bash-3.00$ echo $efg
efg
bash-3.00$ echo $hij
hij
bash-3.00$ echo $klm
klm

# 5  
Old 07-27-2011
You can try this
set $bill=`cat file1.txt | tr " " "_" | cut -f1 -d_`
set $height=`cat file1.txt | tr " " "_" | cut -f2 -d_`
set $weight=`cat file1.txt | tr " " "_" | cut -f3 -d_`
# 6  
Old 07-27-2011
Something like this:

Code:
 
TestBox>for word in `cat input_file`
> do
> eval $word="$word"
> export $word
> done
 
echo $ani
ani
 
echo $bill
bill

# 7  
Old 07-28-2011
@arup
Your answer will work for a specific entries in file1, however i am expecting a generic method.
But still, thanks for your effort SmilieSmilie

---------- Post updated 07-28-11 at 09:35 AM ---------- Previous update was 07-27-11 at 04:13 PM ----------

@itkamaraj
Thanks, your code works. I am trying to understand the logic behind your code.

can you explain the following points:

Code:
awk '{for(i=1;i<=NF;i++) printf("%s=%s\n",$i,$i)}' test > test.var && . test.var

Please explain what the test.var && . test.var signify?

Last edited by animesharma; 07-28-2011 at 01:05 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

2. Solaris

Extract content of .dump file

We have been provided a .dump file.The need is to extract the contents(may includes files and folder). ls -lZ didnt help me as Z is not a valid option. How to extract the file contents ? (7 Replies)
Discussion started by: vinil
7 Replies

3. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

4. Shell Programming and Scripting

Extract Content from a file

I have an input file with contents like: ./prbru6/12030613.LOG:24514|APPL|prbru6.8269.RTUDaemon.1|?|13:49:56|12/03/06|GMT+3|?|RTUServer Error:Count of Internal Error Qty (-1) < 0, for Audit group id - 1L5XVJ6DQE36AXL, after record number,1, File: EventAuditor.cc, Line: 394|? ... (5 Replies)
Discussion started by: rkrish
5 Replies

5. Shell Programming and Scripting

perl extract content of file

I'm using Mail::Internet module, which will basically filter through email content and extract the body of the message my perl script to extract the body of the email #!/usr/bin/perl -w use Mail::Internet; @lines = <STDIN>; $mi_obj = new Mail::Internet(); ... (2 Replies)
Discussion started by: amlife
2 Replies

6. Shell Programming and Scripting

Extract XML content from a file

310439 2012-01-11 03:44:42,291 INFO PutServlet:? - Content of the Message is:="1.0" encoding="UTF-8"?><ESP_SSIA_ACC_FEED> 310440 <BATCH_ID>12345678519</BATCH_ID> 310441 <UID>3498748823</UID> 310442 <FEED_TYPE>FULL</FEED_TYPE> 310443 <MART_NAME>SSIA_DM_TRANSACTIONS</MART_NAME> 310444... (11 Replies)
Discussion started by: arukuku
11 Replies

7. Shell Programming and Scripting

Extract specific content from a file

My input file: >sequence_1 ASSSSSSSSSSSDDDDDDDDDDDCCCCCCC ASDSFDFFDFDFFWERERERERFSDFESFSFD >sequence_2 ASDFDFDFFDDFFDFDSFDSFDFSDFSDFDSFASDSADSADASD ASDFFDFDFASFASFASFAFSFFSDASFASFASFAFS >sequence_3 VEDFGSDGSDGSDGSDGSDGSDGSDG dDFSDFSDFSDFSDFSDFSDFSDFSDF SDGFDGSFDGSGSDGSDGSDGSDGSDG My... (22 Replies)
Discussion started by: patrick87
22 Replies

8. UNIX for Dummies Questions & Answers

address variable content with csh

By using a csh, I want to address a variable content whose name is/matches the content of a given other variable. i.e. set name=´sam´ set ${name}_age=´27´ So, by typing: echo ${name}_age I correctly obtain: sam_age By typing: echo $sam_age or echo ${sam_age} I correctly obtain: 27 ... (1 Reply)
Discussion started by: sobolev
1 Replies

9. Shell Programming and Scripting

Content extract of a file using awk

Hi Everyone, I have a file with the below content: File1.txt ====== ### ###==> the below table was created for testing1 purpose; ### create table 123 ( field1 date, field2 char(10) primary key(field1) ); ### ###==> the below table was created... (5 Replies)
Discussion started by: nr_shan
5 Replies

10. Shell Programming and Scripting

extract content from a file and insert to another file

please help for the following task... I have to extract the mac address & IP address from the file1: ... 0100004512EEF4 03 192.168.0.7 192.168.0.1 -1 ... 0100779hF5D212 03 192.168.0.8 192.168.0.1 -1 ... 0100789lF5D212 03 192.168.0.9 192.168.0.1 -1 ... ... change the format (addidng... (15 Replies)
Discussion started by: fredao
15 Replies
Login or Register to Ask a Question