wanted to make file content in required format.
Hi,
I do have one file which contains some data in format as.... pqrs abcd efgh xyz .... so i wanted to make that everything should be in a single line as below format like .. 'pqrs','abcd','efgh','xyz',..... means each word should be in a single line with seprator as , and should be quoated with single quoate. i was trying with sed but single quoate creating a problem..i guess single quote doesnt work properly with sed utility.. so any other way to make in this format... any other way to make this... |
Show us your attempt so we may assist in a solution?
|
so the file contents are..
123.gzip 234.gzp 356.gzip and iam trying to do like '123.gzip','234.gzip','356.gzip' by command sed -e 's/[0-9]*$/'&'/g' filename but not working.. |
hmm...
try this one.. awk '{printf "\x27%s\x27,", $1}' data where data is the file that contains your list.. was an interesting one.. |
Have ...
Code:
123.gzip Code:
'123.gzip','234.gzip','356.gzip' Code:
sed "s/^/'/; s/$/'/" $InFile |paste -s -d"," |
Minor nitpick
Quote:
Code:
'123.gzip','234.gzp','356.gzip' Code:
'123.gzip','234.gzp','356.gzip', Daniel B. Martin |
Thanks! Sangfriod its worked out:cool:
can you please elaborate awk '{printf "\x27%s\x27,", $1}' .How it works out... or redirect please redirect me to the reference page Thanks Again! |
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.
|
Here is an all sed:
Code:
sed -r "s/.*/'&'/;:a N;s/(.*)\n(.*)/\1,'\2'/;ta" file |
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: Here are a few useful sed references: http://www.grymoire.com/Unix/Sed.html http://sed.sourceforge.net/sedfaq.html http://sed.sourceforge.net/grabbag/ http://sed.sourceforge.net/sed1line.txt http://www.catonmat.net/series/sed-one-liners-explained BTW, here's an ed solution for you too. Since it's a full-fledged text editor, it's able to do many multi-line operations more easily than sed. Code:
printf '%s\n' "%s/.*/'&'/" '%-1s/$/,/' '%j' 'w newfile.txt' | ed -s oldfile.txt You can insert a '%p' (print all) command to view the entire file at any step. How to use ed: http://wiki.bash-hackers.org/howto/edit-ed http://snap.nlc.dcccd.edu/learn/nlc/ed.html (also read the info page) |
Test results and questions
These tests ...
Code:
echo; echo "Method of LQ Newbie sangfroid" Code:
Method of LQ Newbie sangfroid Question 2) Why did David the H.'s solution fail? Daniel B. Martin |
Hmm. What does the input look like, exactly?
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. |
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" 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 |
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.
|
Improved solution
I "polished" my code and now have this ...
Code:
sed "s/.*/'&'/" $InFile |paste -sd, > $OutFile It's also the most readable, though readability is subjective. I really like paste because, in one swoop, it ... - inserts commas where they are wanted - removes linefeeds where they are not wanted. Daniel B. Martin |
All times are GMT -5. The time now is 05:04 AM. |