Extract information from txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract information from txt file
# 8  
Old 11-09-2016
Did you consider reading an array? That would require a recent shell, though, your version of which you failed failed to mention.

Last edited by RudiC; 11-09-2016 at 07:35 AM..
# 9  
Old 11-09-2016
Code:
$ cat tst.cfg
AA BC FG
RF TT GH NL
DD FF HH

Code:
$ cat mtst
#!/bin/bash
while read a
do set -- $a
i=0
while [ $i -lt $# ]
do let i=i+1
eval echo -n var$i=\$$i'\ '
done
echo
done <tst.cfg

Code:
$ sh mtst
var1=AA var2=BC var3=FG
var1=RF var2=TT var3=GH var4=NL
var1=DD var2=FF var3=HH
$

Feel free to adjust the code to your platform & needs
# 10  
Old 11-09-2016
If you are sure of just three fields, you could:-
Code:
while read var1 var2 var3 extra
do
   echo "\$var1=$var1"
   echo "\$var2=$var2"
   echo "\$var3=$var3"
   if [ ! -z "$extra" ]
   then
      echo "I have extra fields \"$extra\""
   fi
done < input_file

You would need to work with the variables within the loop and depending on your shell, the last values set may or may not persist after the loop terminates (i.e. it may read nulls into the variables as it tries to read beyond the end of file.



I hope that this helps,
Robin
# 11  
Old 11-09-2016
You can try the below one as well ( in bash ) .

Code:
while read -a line; do for i in ${line[@]}; do echo -n "var$x:$i "; ((x++)); done; echo "";x=1;done < file

# 12  
Old 11-09-2016
With a recent shell:
Code:
while read -a VARR; do for i in ${!VARR[@]}; do echo -n "var$i:${VARR[$i]} "; done; echo;  done < file3
var0:AA var1:FG var2:TH 
var0:AD var1:FR var2:HK var3:LO 
var0:AX var1:DF var2:ER var3:TR 
var0:AZ var1:SD var2:WE

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract information from file

In a particular directory, there can be 1000 files like below. filename is job901.ksh #!/bin/ksh cront -x << EOJ submit file=$PRODPATH/scripts/genReport.sh maxdelay=30 &node=xnode01 tname=job901 &pfile1=/prod/mldata/data/test1.dat ... (17 Replies)
Discussion started by: vedanta
17 Replies

2. Shell Programming and Scripting

Extract information from file

Gents, If is possible please help. I have a big file (example attached) which contends exactly same value in column, but from column 2 to 6 these values are diff. I will like to compile for all records all columns like the example attached in .csv format (output.rar ).. The last column in the... (11 Replies)
Discussion started by: jiam912
11 Replies

3. Shell Programming and Scripting

How to extract information from a file?

Hi, i have a file like this: <Iteration> <Iteration_iter-num>3</Iteration_iter-num> <Iteration_query-ID>lcl|3_0</Iteration_query-ID> <Iteration_query-def>G383C4U01EQA0A length=197</Iteration_query-def> <Iteration_query-len>197</Iteration_query-len> ... (9 Replies)
Discussion started by: the_simpsons
9 Replies

4. UNIX for Dummies Questions & Answers

How can i sort a .txt file without loosing the header information?

Hi, I'm trying to sort 2 different .txt tab delimited files with the command line: sort -k 1b,1 inputfile > outputfile But doing that i'm also sorting the header (that ends at the end of my file). How can i sort a .txt file without sorting the header but conserving the header in the... (3 Replies)
Discussion started by: alisrpp
3 Replies

5. Shell Programming and Scripting

Command to extract all columns except the last few from a txt file

hello, i have publicly available txt file with little less than 300000 rows. i want to extract from column 1 to column 218 and save it in another text file. i use the cut command but the file is saved with multiple rows from the source file onto a single row in the destination. basically it is... (6 Replies)
Discussion started by: madrazzii
6 Replies

6. Shell Programming and Scripting

How to read userid and password information from txt file

Hi Experts, I am writing a shell script (for displaying disk space details) which is logging to 15 different servers using following command. ssh userid@servername It is prompting me for password for all 15 servers when I manually run it. However , soon I would like to schedule this script... (4 Replies)
Discussion started by: ajaypatil_am
4 Replies

7. Shell Programming and Scripting

Extract various information from a log file

Hye ShamRock If you can help me with this difficult task for me then it will save my day Logs : ================================================================================================================== ... (4 Replies)
Discussion started by: SilvesterJ
4 Replies

8. UNIX for Dummies Questions & Answers

Extract numbers from .txt file

I need to extract all the p-value numbers and the rho numbers from a .txt file and write them as coma separated values in a new file. Ideally I would get two files in the end, one for p- values and one for rho. Any suggestions? I appreciate your help!!! The .txt file looks essentially like this... (5 Replies)
Discussion started by: eggali
5 Replies

9. Shell Programming and Scripting

Extract from txt file

I have data as follow in the txt file. I want to skip line starting with '#' sign. #command program abc defmt exp refmt ... ... I want to store abc exp .... in a array. I want to store defmt refmt in a array I need command to read each line in the file. I need... (6 Replies)
Discussion started by: ekb
6 Replies

10. Shell Programming and Scripting

extract and format information from a file

Hi, Following is sample portion of the file; <JDBCConnectionPool DriverName="oracle.jdbc.OracleDriver" MaxCapacity="10" Name="MyApp_DevPool" PasswordEncrypted="{3DES}7tXFH69Xg1c=" Properties="user=MYAPP_ADMIN" ShrinkingEnabled="false" ... (12 Replies)
Discussion started by: sujoy101
12 Replies
Login or Register to Ask a Question