Read file and for each line replace two variables, add strings and save output in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read file and for each line replace two variables, add strings and save output in another file
# 1  
Old 03-01-2011
Read file and for each line replace two variables, add strings and save output in another file

Hi All,

I have a file, let's call it "info.tmp" that contains data like this ..
Code:
ABC123456
PCX333445
BCD789833

I need to read "info.tmp" and for each line add strings in a way that the final output is

Code:
put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)'

where XXX are the first three characters of each line in "info.tmp" and
xxxxxx are the remaining 6 characters of each line in "info.tmp"

So something like ...

Code:
GRP="ABC"
SITE="123456"
echo "put /logs/ua/dummy.trigger 'AAA00001.FTP."$GRP".BLA03A01."$SITE"(+1)'" >> trigger.tmp

But considering that I need this line created for each row in "info.tmp" file!

I've tried with

Code:
for GRP in `cat info.tmp | cut -c1-3`
do
    for SITE in `cat info.tmp | cut -c4-9`
    {
       echo "put /logs/ua/dummy.trigger 'AAA00001.FTP."$GRP".BLA03A01."$SITE"(+1)'" >> trigger.tmp
    }
done

but of course this duplicates records ... because for each GRP it matches every SITE.

I've also tried

Code:
awk '{print substr($0,1,3)}'
awk '{print substr($0,4,9)}


but I don't know how to "concatenate" this result with the other "echoes" I need and I know that after an awk is used, if you use another one, you won't have the full string you had at the first time. Also if I "echo" then the "awk" would take that string and not the content of the file "info.tmp".

Help will be much appreciated!
# 2  
Old 03-01-2011
Code:
nawk -v q="'" '{print "put /logs/ua/dummy.trigger " q "AAA00001.FTP." substr($1,1,3) ".BLA03A01." substr($1,4) "(+1)" q}' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-01-2011
This works!

Thank you, vgersh99!!

This does exactly what I need but unfortunately, my system doesn't support nawk! So I tried it with awk and it works too!

Thank you, thank you, thank you!
# 4  
Old 03-01-2011
You can also do with with pure bash/ksh shell (You were pretty close with your initial attempt):

Code:
while read LINE
do
    SITE=${LINE#???}
    GRP=${LINE%$SITE}
    echo "put /logs/ua/dummy.trigger 'AAA00001.FTP."$GRP".BLA03A01."$SITE"(+1)'"
done < info.tmp >> trigger.tmp

# 5  
Old 03-02-2011
One way through sed..
Code:
sed "s|\(...\)\(.*\)|put /logs/ua/dummy.trigger 'AAA00001.FTP.\1.BLA03A01.\2(+1)'|" inputfile > outfile

# 6  
Old 03-03-2011
Thanks a lot, everyone

Thank you!
All of these worked perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. UNIX for Beginners Questions & Answers

Read line and save fields as variables

Hej guys, I am trying to read a csv file line by line, save it's fields as variables per line so I can use them as parameters and execute stuff. I am new to shell scripting and was just strictly following a tutorial. Somehow my version seems to ignore the loop. Any help? TY! :) #!/bin/bash... (12 Replies)
Discussion started by: Splinter479
12 Replies

3. UNIX for Dummies Questions & Answers

Read in Multiple log files and output selected variables and values to cvs file

I have several problems with my problems: I hope you can help me. 1) the If else statement I am getting an error message. My syntax must be incorrect because the entire statement is throwing an error. For example in filew.log if these items don't exist Memsize, SASFoundation and also if... (0 Replies)
Discussion started by: dellanicholson
0 Replies

4. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

5. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

6. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

7. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

8. Shell Programming and Scripting

Read file and replace a particular line if found

Hi All There is another challenge which stand in front of me. And want all to have the experience with that I have a file in Unix say a.txt. What I was trying is to read the file line by line and matching the line to particular pattern, and if that pattern found I want to replace that line... (5 Replies)
Discussion started by: adisky123
5 Replies

9. Shell Programming and Scripting

How to add data from 2 input files and save it in 1 output file

Hi, i have 2 input files which are file1.txt and file2.txt. I need to extract data from file1.txt and file2.txt and save it in file3.txt like example below:- File1.txt ID scrap1 Name scrap1 start 1 end 10 ID scrap2 Name scrap2 start 11 end ... (4 Replies)
Discussion started by: redse171
4 Replies

10. Shell Programming and Scripting

Read 1-line file and separate into multiple variables

I have one line files with 17 records separated by a semi-colon. I need to create a variable from each record, which I can do via a separate awk for each one, but I know there has to be a better way. Along with pulling out the variable, I need to convert some url coding like a + to a space, etc.... (4 Replies)
Discussion started by: numele
4 Replies
Login or Register to Ask a Question