Visit Our UNIX and Linux User Community


Need help to read rows from one file and concatenate to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to read rows from one file and concatenate to another
# 1  
Old 07-22-2010
Need help to read rows from one file and concatenate to another

Hi guys;

TBH I am an absolute novice, when it comes to scripting; I do have an idea of the basic commands...

Here is my problem;

I have a flatfile 'A' containing a single column with multiple rows. I have to create a script which will use 'A' as input and then output a string in in the format ('row1','row2','row3')
eg:
file A:
Y781
TGH8
UIP9

output:
('Y781','TGH8','UIP9')

NB: No, I'm not a college student asking for help on his coding assignment!

Last edited by carlos_anubis; 07-22-2010 at 10:41 AM.. Reason: Clarifying nature of source file
# 2  
Old 07-22-2010
You can do something like that :
Code:
awk -v Q="'" '
BEGIN {
   ORS = "";
   print "(";
}
{
   print (NR>1 ? "," : "") Q $0 Q;
}
END {
   print ")\n";
}' inputfile

Inputfile:
Code:
Y781
TGH8
UIP9

Output:
Code:
('Y781','TGH8','UIP9')

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 3  
Old 07-22-2010
Code:
awk 'BEGIN {printf("(");}
       {printf"("'%s'," )  }
       END {printf(")\n") } ' inputfile > outputfile

See how that does for you
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 07-22-2010
Quote:
Originally Posted by jim mcnamara
Code:
awk 'BEGIN {printf("(");}
       {printf"("'%s'," )  }
       END {printf(")\n") } ' inputfile > outputfile

See how that does for you
Jim, there is a problem with your script
Code:
$ awk 'BEGIN {printf("(");}
>        {printf"("'%s'," )  }
>        END {printf(")\n") } ' 
 awk: The string  )  } cannot contain a newline character.
 The source line is 2.
 The error context is
                       {printf"("%s," )  } >>> 
 <<< 
 syntax error The source line is 3.
 awk: The statement cannot be correctly parsed.
 The source line is 3.
        awk: There is a missing } character.

and the argument of the printf is missing after the format string.

Another problem is that there will be an unwanted comma before the ) in the resulting output.

Jean-Pierre.
# 5  
Old 07-22-2010
Yeah, the script does have a problem as mentioned by Jean-Pierre in the below post... but thank you for trying to solve my problem!
# 6  
Old 07-22-2010
Hi

Code:
awk '$0=q $0 q' q="'" infile | paste -s -d, | sed 's/.*/(&)/'

Guru.
# 7  
Old 07-22-2010
Code:
# cat infile
Y781
TGH8
UIP9

Code:
# echo "('"$(sed -n "N;N;s/\n/','/gp" infile )"')"
('Y781','TGH8','UIP9')


Previous Thread | Next Thread
Test Your Knowledge in Computers #711
Difficulty: Easy
The earliest computing machines had fixed programs.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read and concatenate content file and file name

Hi all, i need a bash script. I have a 3 file named Milano, Torino, Firenze Into file i have: Milano Marco Luca Giorgio Michele PatrizioTorino Marco Giulio Emilio MicheleFirenze Luca Giorgio Marco Saverio EmilioThe output should be a all_city.csv file like: (3 Replies)
Discussion started by: kamose
3 Replies

2. Shell Programming and Scripting

Concatenate rows and redefine range

I'm trying to find a way to concatenate consecutive rows (key is column $1 and $2) if column $5 an $6 are integers and redefine ranges in columns $3&$4 and $5&$6 Unfortunately I'm still learning the very basics so I cannot figure a way of doing this with awk. Input file 15 30 21 21 25.0... (11 Replies)
Discussion started by: alex2005
11 Replies

3. Shell Programming and Scripting

Concatenate broken rows

I need to concatenate the rows that are broken (because of carriage return and line feed) in unix. Input 123|456|789|"" 987|786|"GRT "|"" 3455|896|654|456|"" 457|234|"RT"|"PR TY"|"" Output 123|456|789|"" 987|786|"GRT"|"" 3455|896|654|456|"" 457|234|"RT"|"PRTY"|"" (16 Replies)
Discussion started by: meet_calramz
16 Replies

4. Shell Programming and Scripting

Read column from file and delete rows with some condition..

Hi.... I have a need of script to do delete row whenever condition is true.... 2.16 (3) 1 3 9999 0 (1) (0) 34.42 (4) 1 3 9999 37 (2) (3) 34.38 (4) 1 3 9999 64 (2) (3) 34.4 (4) 1 3 1 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

5. UNIX for Dummies Questions & Answers

Read rows from source file and concatenate output

Hi guys; TBH I am an absolute novice, when it comes to scripting; I do have an idea of the basic commands... Here is my problem; I have a flatfile 'A' containing a single column with multiple rows. I have to create a script which will use 'A' as input and then output a string in in the... (0 Replies)
Discussion started by: carlos_anubis
0 Replies

6. Shell Programming and Scripting

Concatenate some of the rows...

i have a file as below and i need to load it into oracle. The problem is, some of the rows are in 2 lines. 123456_PosWlist ----- ----- IN 0/0 123456_PosWListRpt ----- ----- IN 0/0 123456_PosWListCSV ----- -----... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

7. Shell Programming and Scripting

concatenate two files with different No of rows

need a shell which perform following function file 1 ( every time new data comes) 1212 2323 3434 4545 5656 . . . . file 2 (fixed line) update bc_tbl set aix=data , bix=back where cix=U and serial=; now when i execute shell it will concatinate file 1, file 2 & make file 3 as... (3 Replies)
Discussion started by: The_Archer
3 Replies

8. Shell Programming and Scripting

read file and print additional rows till current year

Hi all i have a file like 2006,1,2 2007,2,3 2008,3,4 I will read this and my output should be like 2006,1,2 2007,1,2 2008,1,2 2007,2,3 2008,2,3 2008,3,4 Giving the explanation, we will read the first line of the file and if the year any other than current year, we will print as many... (1 Reply)
Discussion started by: vasuarjula
1 Replies

9. Shell Programming and Scripting

Concatenate rows in to 2 files

I have 2 files FILEA 1232342 1232342 2344767 4576823 2325642 FILEB 3472328 2347248 1237123 1232344 8787890 I want the output to go into a 3rd file and look like: FILEC 1232342 3472328 (1 Reply)
Discussion started by: unxusr123
1 Replies

10. Shell Programming and Scripting

Concatenate 2 rows into 1 row

I need to search a file for two values (valueA & valueB). ValueA will be on a different row than valueB, and concatenate the two together on the same row of my output. Example: search input file for strings "node" and "OS", combine the two results into one row input node A text text OS... (4 Replies)
Discussion started by: indianadoug
4 Replies

Featured Tech Videos