Reading value from one file & using it for 2nd file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading value from one file & using it for 2nd file
# 1  
Old 04-21-2005
Reading value from one file & using it for 2nd file

In File A I have 1st col as numbers. Using these numbers, I need to find out
the rows at this numbered index in other file.

I know loose commands.
To get the 1st col from file A :
awk '{print $1}' fileA

To get the row at the index, say 100 - from file B :
sed '100p' fileB

After executing 1st command, I get all values in one go, & I cannot use these values
one by one. Can anyone guide on this, regarding how to solve this?
# 2  
Old 04-21-2005
Assume that the files are named fileA and fileB. You can write the commands as:

Code:
awk '{print $1}' fileA | while read indx; do
sed '${indx}p' fileB
done

Just try if this works. I am just a bit doubtful about the ${indx} inside the sed command.

Hope this helps!

Cheers!
# 3  
Old 04-21-2005
Code:
awk '{print $1}' fileA | while read indx; do
sed "\${indx}p" fileB
done

# 4  
Old 04-22-2005
Either sed '${indx}p' or sed '/${indx}p'

is not working as expected. Can anyone please suggest how we can assign variable with this command?
# 5  
Old 04-22-2005
Try:
sed "${indx}p" fileB

Note the double quotes. Double quote is the character above single quote on your keyboard.
# 6  
Old 04-22-2005
Quote:
Originally Posted by videsh77
Either sed '${indx}p' or sed '/${indx}p'

is not working as expected. Can anyone please suggest how we can assign variable with this command?
read more closely the suggestion.
# 7  
Old 04-22-2005
Quote:
Originally Posted by vgersh99
read more closely the suggestion.
Notice the backslash in front of the dollar sign which is quoting the dollar sign and preventing the indx variable from being referenced.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. Shell Programming and Scripting

File Move & Sort by Name - Kick out Bad File Names & More

I have a dilemma, we have users who are copying files to "directory 1." These images have file names which include the year it was taken. I need to put together a script to do the following: Examine the file naming convention, ensuring it's the proper format (e.g. test-1983_filename-123.tif)... (8 Replies)
Discussion started by: Nvizn
8 Replies

3. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

4. UNIX for Dummies Questions & Answers

Reading the dates from a file & moving the files from a directory

Hi All, I am coding for a requirement where I need to read a file & get the values of SUB_DATE. Once the dates are found, i need to move the files based on these dates from one directory to another. ie, this is how it will be in the file, SUB_DATE = 20120608,20120607,20120606,20120606... (5 Replies)
Discussion started by: dsfreddie
5 Replies

5. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

6. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

7. Shell Programming and Scripting

Test File Reading & Validation using Shell script

Please help develop script for below requirement -------Sample file------------------------------- HSVSHOSTRECON 20090115011817BP DARMAR60064966247003504720000000000000000000066626000000000000133000003D003463001332 ... (14 Replies)
Discussion started by: niraj_bhatt
14 Replies

8. Shell Programming and Scripting

cp && rm command, rm: <file> not removed. No such file or directory

Hi, I am running this is korn shell cp $source/$fname $dest/dir && rm $source/$fname This was returned: rm: /dir/file not removed: No such file or directory The file could be found in the $dest directory which meant the cp was success. The above code is used in a for loop to move... (5 Replies)
Discussion started by: Leion
5 Replies

9. Shell Programming and Scripting

Arrays & File Reading

Ok; here is the code INCREMENT=0 #Final Count MATCH=0 #Treated as a Boolean declare -a LINEFOUR #Declared Array for FILE in $DIR; do # DIR was declared earlier test -f $FILE && ( TEMP=(sed -n '4p' $FILE) #How do I assign the fourth line of the file to TEMP? This doesn't... (1 Reply)
Discussion started by: Asylus
1 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question