Convert


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Convert
# 1  
Old 08-20-2014
Convert

my file or content of the file looks like this:
Code:
HR-BUSPARTNET
SC-POINQUIRY
BATCH
FIN-CHOPDATARESTRICT
HR-DATABASEACCESSNOEXEC
GL-EXTINQ
RSS
SC-APPROVER
ESS


all said and done, i'd like to have it look like this:

Code:
HR-BUSPARTNET,SC-POINQUIRY,BATCH,FIN-CHOPDATARESTRICT,HR-DATABASEACCESSNOEXEC,GL-EXTINQ,RSS,SC-APPROVER,ESS

---------- Post updated at 09:47 PM ---------- Previous update was at 09:37 PM ----------

never mind, awk -v ORS="," '1' inputfile, solved.

Last edited by Don Cragun; 08-20-2014 at 11:24 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-20-2014
Consider piping to following sed command to replace the obligatory trailing comma with a newline character:
| sed "s/,$/\n/"

A non-awk approach for the posterity: $ tr '\n' ',' <input | sed "s/,$/\n/" >output
# 3  
Old 08-20-2014
To: lawsongeek
The file produced by:
Code:
awk -v ORS="," '1' inputfile

not only includes an unwanted trailing comma, it is also missing a trailing <newline> character. Therefore, by definition, the output is not a text file. This might or might not be important depending on what you are going to do with the output from this command.

To: junior-helper
The code:
Code:
tr '\n' ',' <input | sed "s/,$/\n/" >output

might work on some systems, but it is not portable. The output of the tr command will not have a trailing <newline> character (so by definition, the output is not a text file). And, according to the standards, the behavior of sed is unspecified if any of its input files is not a text file.

A couple of ways to portably merge a file containing one or more lines into a single line text file are:
Code:
awk '
FNR==2 { d = "," }
{ printf("%s%s", d, $0)}
END { print ""}' input > output

and:
Code:
(tr '\n' ',' <input; echo) | sed "s/,$//" >output

Note, however, that if the size of the input file is larger than the LINE_MAX limit on your system (which can be as small as 2048 bytes), the resulting output file still will not be a text file (and in the 2nd pipeline, the behavior of sed is still unspecified if the size of the input file is LINE_MAX or larger).
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-21-2014
Hello lawsongeek,

One more approach not effective as Don's but may help.
Thanks Don for nice approach.

Code:
awk '{for(i=1;i<=NR;i++){a=a ORS $0;next}} END{gsub(/^[[:space:]]/,Y,a);gsub(/[[:space:]]/,",",a); print a}' filename

Output will be as follows.

Code:
HR-BUSPARTNET,SC-POINQUIRY,BATCH,FIN-CHOPDATARESTRICT,HR-DATABASEACCESSNOEXEC,GL-EXTINQ,RSS,SC-APPROVER,ESS


Thanks,
R. Singh
# 5  
Old 08-21-2014
This is a variant of the efficient one that prints the last newline only if the inputfile is not empty.
Code:
awk '
{ printf ("%s%s", d, $0); d="," }
END { if (NR>0) print ""}' input > output

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 08-21-2014
Quote:
Originally Posted by RavinderSingh13
Hello lawsongeek,

One more approach not effective as Don's but may help.
Thanks Don for nice approach.

Code:
awk '{for(i=1;i<=NR;i++){a=a ORS $0;next} END{gsub(/^[[:space:]]/,Y,a);gsub(/[[:space:]]/,",",a); print a}' filename

... ... ...

Thanks,
R. Singh
Hi RavinderSingh,
If you want to take this approach, wouldn't this be more efficient:
Code:
awk '{a=(a==""?"":a",")$0;next}} END{print a}' filename

I don't see the need for the for loop; did I miss something?

The reason I didn't use this approach is that the script I suggested before will produce the desired output successfully even if the total length of the output is longer than LINE_MAX bytes as long as no single input line is longer than LINE_MAX. The behavior with this script is again unspecified if the length of the string stored in a is (LINE_MAX -1) or more bytes (which will happen whenever the size of the input file is LINE_MAX or more bytes).
----------------------------------------------
Update: Removed extraneous } as suggested by RavinderSingh13 in next message.

Last edited by Don Cragun; 08-21-2014 at 07:53 AM.. Reason: Fix typo }} -> }
# 7  
Old 08-21-2014
Posted by Don:

Quote:
Quote:
Originally Posted by RavinderSingh13 Image
Hello lawsongeek,

One more approach not effective as Don's but may help.
Thanks Don for nice approach.


Code:
awk '{for(i=1;i<=NR;i++){a=a ORS $0;next}} END{gsub(/^[[:space:]]/,Y,a);gsub(/[[:space:]]/,",",a); print a}' filename


... ... ...

Thanks,
R. Singh

Hi RavinderSingh,
If you want to take this approach, wouldn't this be more efficient:

Code:
awk '{a=(a==""?"":a",")$0;next}} END{print a}' filename

I don't see the need for the for loop; did I miss something?

The reason I didn't use this approach is that the script I suggested before will produce the desired output successfully even if the total length of the output is longer than LINE_MAX bytes as long as no single input line is longer than LINE_MAX. The behavior with this script is again unspecified if the length of the string stored in a is (LINE_MAX -1) or more bytes (which will happen whenever the size of the input file is LINE_MAX or more bytes).
Thank you Don for really nice solution, but seems there is an extra } character, so removed the same now.

Code:
awk '{a=(a==""?"":a",")$0;next} END{print a}' filename


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Convert .sh to .bat please

Can someone translate this code to .bat for me? I have no clue. #!/bin/sh WAS_HOME="/opt/websphere/appserver/profiles/AppSrv01" WAS_APP_SERVER="server1" WAS_PROFILE_NAME="AppSrv01" echo "Stopping App Server" "${WAS_HOME}/bin/stopServer.sh" -profileName $WAS_PROFILE_NAME $WAS_APP_SERVER... (1 Reply)
Discussion started by: Blogger11
1 Replies

2. Shell Programming and Scripting

Convert / to \

Hi, In my input variable values are c:/test/sample/ I need to convert it to c:\test\sample I need to find and replace it How do I do it? var_conversion=`"$var_SearchFile" | sed 's/'\'/'/'/g'` echo ${var_conversion%/*} My code throws error (4 Replies)
Discussion started by: magesh_bala
4 Replies

3. Shell Programming and Scripting

Convert date

If I pass the value in the form YYYY/MM/DD(20100719) how to convert in the form 19\/Jul\/2010 (1 Reply)
Discussion started by: sandy1028
1 Replies

4. Shell Programming and Scripting

Convert e+ to number

Hi, I am using awk to get particular dates in seconds and the output am getting is like 1.28071e+09. How can I convert it to number format. Can anyone help me out? Thanks in advance..! (7 Replies)
Discussion started by: Kattoor
7 Replies

5. Shell Programming and Scripting

help to convert

how to convert this perl expression to Python while (<FIL>) { s/+/ /g; if (/<DOC\s+doc_ID=\"(.*?)\".*sys_ID=\"(.*?)\"/i) { #" ${$$hashptr{$sys_Id}}{$doc_Id} = $docstr; # push(@$doclistptr, $doc_Id); $doc_Id = $1; $sys_Id = $2; $docstr = ""; ... (1 Reply)
Discussion started by: jaganadh
1 Replies

6. Shell Programming and Scripting

convert to GB

hello, my objective is to calculate the swap size -bash-3.00# swap -l swapfile dev swaplo blocks free /dev/md/dsk/d1 85,1 8 33559776 16966160 so size=blocks*512/(1024*1024*1024) since blocks are 512-blocks any idea how to get it? so far I was able to... (6 Replies)
Discussion started by: melanie_pfefer
6 Replies

7. Programming

convert without using strtol

Hi I need help with some function. I have to fetch data from stdin without using of scanf() (I have to use getc or getchar) and then each sign I want to convert to another sign - here I can't use function strtol :( . In the end i want to print this string to stdout without using of function printf... (3 Replies)
Discussion started by: BMW750LIuser
3 Replies

8. Shell Programming and Scripting

here-doc convert 2 script convert to single script?

I have my main script calling another script to retrive a "ls -alt" of a directory that's located in a remote location I'm sftping into. main.sh #!/bin/ksh getLS.sh > output.txt getLS.sh #!/bin/sh /home<..>/sftp <host@ip> <<! cd /some/dir/Log ls -alt quit ! Basically I'd like to be... (2 Replies)
Discussion started by: yongho
2 Replies

9. Shell Programming and Scripting

Convert database

We use the MS access database, I want to convert the database to mysql db , could suggest is there any tools / methods that I can export the table and data from access , and import it to the new mysql db ? thx in advance. (3 Replies)
Discussion started by: ust
3 Replies

10. UNIX for Dummies Questions & Answers

To convert

Hi, I have a source file with 'Aug 1 2004' kind format. I need to search the records with certain period, like from Aug 1 2004 to Sep 20 2004. Can someone give me some ideas what I should start with? ThanX !! (1 Reply)
Discussion started by: whatisthis
1 Replies
Login or Register to Ask a Question