retrieve string from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting retrieve string from file
# 1  
Old 12-29-2009
retrieve string from file

hi, I have write a code to retrive data from each line of a file:

Code:
sed -e '/^#/d' file.csv | awk '{ printf "TEST,%s:AUX,%s;\n", $0, "'A'"}' > 
pippo.txt

where the input file.csv was like this:
Code:
1234
2345
2334
3344

and the output of my code is a file with:
Code:
TEST,1234:AUX,A;
TEST,2345:AUX,A;
TEST,2334:AUX,A;
TEST,3344:AUX,A;

###############################
Now I have a different input file
Code:
1234,3
2345,1
2334,2
3344,1

And for each line I need to retrive the first and the second number(the two separeted by the comma)

I need an output like this:
Code:
TEST,1234:AUX,3;
TEST,2345:AUX,1;
TEST,2334:AUX,2;
TEST,3344:AUX,1;

I'm having a lot of problems writing this new code, can somebody help me?

Last edited by Franklin52; 12-29-2009 at 04:41 PM.. Reason: Please use code tags!!
# 2  
Old 12-29-2009
This might help

awk '{ FS="," } { print "TEST,"$1":AUX,"$2";" }' filename
# 3  
Old 12-29-2009
retrive string from file

Code:
while read line
do
        echo $line | awk -F, '{print "TEST,"$1":AUX,"$2";"}'

done < pippo.txt

i hope it will work fine.

Last edited by Franklin52; 12-29-2009 at 04:43 PM.. Reason: Please use code tags!!
# 4  
Old 12-29-2009
Using sed
Code:
sed 's/\(.*\),\(.*\)/TEST,\1:AUX,\2;/' file

or a loop
Code:
while IFS="," read a b;do echo "TEST,$a:AUX,$b;";done <file

# 5  
Old 12-29-2009
posted in error

Last edited by steadyonabix; 12-29-2009 at 04:25 PM..
# 6  
Old 12-29-2009
Code:
printf "TEST,%s:AUX,%s;\n" $(sed 's/,/ /g' filename)

# 7  
Old 12-29-2009
Or using Perl:

Code:
$
$ cat f7
1234,3
2345,1
2334,2
3344,1
$
$ perl -F, -lane 'print "TEST,$F[0]:AUX,$F[1];"' f7
TEST,1234:AUX,3;
TEST,2345:AUX,1;
TEST,2334:AUX,2;
TEST,3344:AUX,1;
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

2. Shell Programming and Scripting

How to retrieve a number or string from file1 and redirect into file2 in perl script?

hello forum members, I am siva ,As i am new to perl scripting i looking help from forum members. i need a sample program are command for pattern matching. I have file name infile1 which some data, I need to search the particular number are string in the file which repeats n number of... (0 Replies)
Discussion started by: workforsiva
0 Replies

3. UNIX for Dummies Questions & Answers

Hot to retrieve *.sql file names which we refer in .sh file.

Hi Guys, How to retrieve/get *.sql file names which we refer in all *.sh files. Can any one help me on this. Thanks, Kolipaka (3 Replies)
Discussion started by: lakshmanrk811
3 Replies

4. Shell Programming and Scripting

How to retrieve string which does not contain '$'?

Hi, I have a file say file1.ksh. Which has data like: ifile $AI_SERIAL/$FILE.DAT... ofile $AI_SERIAL/feed.dat... My requirement is to find the count of all the lines which does not have $ after /. So i have written the code: grep -w 'AI_SERIAL' file1.ksh | cut -d '/' -f2 | grep... (9 Replies)
Discussion started by: Kamna
9 Replies

5. Shell Programming and Scripting

Help to retrieve data from two files matching a string

Hello Experts, I have come back to this forum after a while now, since require a better way to get my result.. My query is as below.. I have 3 files -- 1 Input file, 2 Data files .. Based on the input file, data has to be retreived matching from two files which has one common key.. For EX:... (4 Replies)
Discussion started by: shaliniyadav
4 Replies

6. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

7. Shell Programming and Scripting

How to retrieve digital string using sed or awk

Hi, I have filename in the following format: YUENLONG_20070818.DMP HK_20070818_V0.DMP WANCHAI_20070820.DMP KWUNTONG_20070820_V0.DMP How to retrieve only the digital part with sed or awk and return the following format: 20070818 20070818 20070820 20070820 Thanks! Victor (3 Replies)
Discussion started by: victorcheung
3 Replies

8. Shell Programming and Scripting

Retrieve string from each line in a file based on a pattern in Unix

I have a file which contains several lines. Sample content of the file is as below. OK testmessage email<test@123> NOK receivemessage email<123@test> NOK receivemessage email(123@test123) NOK receivemessage email<abc@test> i would like to know by scripting will... (10 Replies)
Discussion started by: ramasar
10 Replies

9. UNIX for Advanced & Expert Users

retrieve the file.

How will retrieve for a particular months file in UNIX say for example from January to February 2008. (1 Reply)
Discussion started by: rajesh08
1 Replies

10. Shell Programming and Scripting

retrieve value from a file

hi i have a cfg file,it contains lpdma520.dev.ipc.us.aexp.com=SUBMCORE.REQUEST.FT lpdma521.dev.ipc.us.aexp.com=SUBMCORE.REQUEST.FTREQ lpdma522.dev.ipc.us.aexp.com=SUBMITSECUREFILEFLOW i am retrieving the values using the function RetrieveCfgvalue() { CFG_VALUE=`grep "$2="... (1 Reply)
Discussion started by: satish@123
1 Replies
Login or Register to Ask a Question