Basic line reading and file merge question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic line reading and file merge question
# 1  
Old 05-16-2012
Basic line reading and file merge question

No doubt these questions have been answered many times, but I struggled to find them - sorry. 2 questions:

1. I wish to read in a file one line at a time and do 'stuff' with it, such as:

Code:
file="tst2"
while IFS= read -r line
do

    echo `wget -qO - http://cactus.nci.nih.gov/chemical/structure/$line/smiles` >> smiles.txt
   
done <"$file"

If 'line' has 6 columns and I want only $4, how do I insert only that column into the web address rather the whole line?


2. If I have 2 files A & B, both having multiple columns, how do I take these 2 files and create a new file with columns in the format A2, B1, A3. Where A2 denotes the 2nd column from file A etc.


Many thanks
# 2  
Old 05-16-2012
1.
Code:
awk '{system ("wget -qO - http://cactus.nci.nih.gov/chemical/structure/" $4 "/smiles")}
' tst2 > smiles.txt

2. Assuming both files have the same numbers of lines:
Code:
awk 'NR==FNR{a[NR]=$1;next}{print $2, a[FNR], $3}' fileB fileA

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 05-16-2012
Thank you for such a quick reply - I should maybe have made my question a bit clearer.

1. This is useful thank-you!
However I was looking to access directly the elements of each line though, so that if for example $4 didn't return what I was after, then I could try $5. Then, depending on what column was finally used I would then print out either $1, $2 or $3 from that same line... plus some more stuff.

I realise now that I could just take each line and put it into a temporary new file and access its elements that way - it would just have been nicer if i was able to use some variable like $(line$2).


2.
Perfect thanks

Last edited by Golpette; 05-16-2012 at 11:17 AM..
# 4  
Old 05-16-2012
You can do something like:
Code:
awk '{
  if($4 == "xx"){
     # actions if $4 == "xx"
  }
}
{
  system ("wget -qO - http://cactus.nci.nih.gov/chemical/structure/" $4 "/smiles")
}
' tst2 > smiles.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. UNIX for Beginners Questions & Answers

Basic Linux command line question

:D:D:D These are list of command i typed on opensuse terminal and evolve lots of doubt around ,that i can't answer. COMMAND 1 linux-xavv:/ # cd COMMAND 2 linux-xavv:~ # Does above command 1 and command two with red labelled sign make different meaning or same . 1 linux-xavv:/... (1 Reply)
Discussion started by: lobsang
1 Replies

3. Solaris

Basic question regarding root file system copy to another disk

Hello, I am creating a new disk using the following command: dd if=/dev/zero of=/export/home/ramdisk/0 bs=512 count=4096k after creating the disk, i tool a ufsdump of a solaris 10 filesytem (disk size 512MB) ufsdump -cvf /export/home/ufsdump/sol_orig /and then restored the dump files onto... (10 Replies)
Discussion started by: Zam_1234
10 Replies

4. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

5. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

6. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

Monitoring a file - Basic Bash Question

*This is not homework I am new to UNIX and want to try this Monitoring a file demo* *If this is the wrong forum please move it - im new to the forums* $1 = the file to be monitored $2 = the time for the file to sleep If the file gets changed (using -nt) it will send my username mail saying... (2 Replies)
Discussion started by: Nolan-
2 Replies

9. UNIX for Dummies Questions & Answers

Basic file processing question

I have a csv file with 3 columns. Column 1 is a date "mm/dd/yyyy", column 2 is a dollar amount (e.g. "100.00") & column 3 in a description of where the transaction took place (e.g. "CHECK CRD PURCHASE 10/07 ACME INC USA") so... "10/01/2009","100.00", "CHECK CRD PURCHASE 10/07 ACME INC USA" I... (1 Reply)
Discussion started by: watingo
1 Replies
Login or Register to Ask a Question