[SOLVED] wanted to make file content in required format.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Thanks! Sangfriod its worked out
can you please elaborate awk '{printf "\x27%s\x27,", $1}' .How it works out...
or redirect please redirect me to the reference page
x27 is the hex code for '. Basically, it is going through each of the line of the file and printing 'line_content (variable $1) and a comma (,) and again apostrophe.... it is doing that thing for each and every line.
The 's'ubstitute command in sed cannot, by default, operate on newlines due to the way it processes input. sed takes each line in turn into its pattern buffer minus the newline, and applies the given expressions to it, then empties the buffer before grabbing the next line. There are never any newline characters in the buffer for it to operate on.
Multi-line editing requires use of 'N' and/or the various hold buffer commands. These commands can put multiple lines into the pattern buffer at the same time, with newlines between them, allowing you to target them.
See the relevant sections of the grymoire and the sedfaq:
The first 's' expression surrounds every line with quotemarks. The second 's' adds a comma to the end of every line except the last. The third 'j'oins all lines together. Finally it 'w'rites the modified buffer to a file, which can be the same as the input file (just leave the filename off).
You can insert a '%p' (print all) command to view the entire file at any step.
echo; echo "Method of LQ Newbie sangfroid"
awk '{printf "\x27%s\x27,", $1}' $InFile > $OutFile
echo "OutFile ..."; cat $OutFile; echo "END"
echo; echo "Method of LQ Member danielbmartin"
sed "s/^/'/; s/$/'/" $InFile |paste -s -d"," > $OutFile
echo "OutFile ..."; cat $OutFile; echo "END"
echo; echo "Method of LQ Guru grail"
sed -r "s/.*/'&'/;:a N;s/(.*)\n(.*)/\1,'\2'/;ta" $InFile > $OutFile
echo "OutFile ..."; cat $OutFile; echo "END"
echo; echo "Method of LQ Guru David the H."
printf '%s\n' "%s/.*/'&'/" '%-1s/$/,/' '%j' 'w newfile.txt' | ed -s $InFile > $OutFile
echo "OutFile ..."; cat $OutFile; echo "END"
Produced these results ...
Code:
Method of LQ Newbie sangfroid
OutFile ...
'123.gzip','234.gzp','356.gzip',END
Method of LQ Member danielbmartin
OutFile ...
'123.gzip','234.gzp','356.gzip'
END
Method of LQ Guru grail
OutFile ...
'123.gzip','234.gzp','356.gzip'
END
Method of LQ Guru David the H.
OutFile ...
END
Question 1) Why did sangfroid's solution omit a linefeed? Is this a bug?
Question 2) Why did David the H.'s solution fail?
Daniel B. Martin
Last edited by danielbmartin; 02-01-2013 at 07:58 AM.
Reason: Minor cosmetic improvements
I tried it on multiple inputs, and they all did just fine, except for one file that had dos-format newlines (I'd forgotten I'd set it as such for a previous test). It worked just fine too, after I converted it back.
Let me play with it.
Edit: Ah, my ed command writes directly to an output file, which I named "newfile.txt". You're trying to redirect the non-existant stdout to a different file.
Change the final command to "w $OutFile" and drop the redirection, or alternately to "%p" to have it print the buffer to stdout instead.
Last edited by David the H.; 02-01-2013 at 08:43 AM.
Reason: follow-ups
To put a multiline input into a single line you can use echo. In this way you are a step ahead in order to apply a simple sed solution, e.g.
Code:
echo $(<file) | sed -r "s/^|$/'/g; s/ /','/g"
However this fails if the original lines contain blank spaces, since they will be changed to ',' as well.
Regarding the awk solution you can easily avoid the trailing comma if you add the separator before the string, except for the first iteration. This is easily done by means of a C-like conditional expression:
Code:
awk 'BEGIN{ q = "\x27" } { s ? s = s q "," q $0 : s = q $0 } END { print s q }' file
This takes care of inner blank spaces, since no substitution is made and the characters or strings are added sequentially.
BTW, as for Q1, the output doesn't have any newlines because sangfroid's printf format string doesn't have any. It's a byproduct of the concatenation process. You can tack on an 'END{ print }' to have it insert a final one.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.