Replace end of file with ','


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace end of file with ','
# 1  
Old 10-05-2016
Replace end of file with ','

Hi,

I have a file with below

Code:
ABC
XYZ
123

I want it to look it like


Code:
'ABC','XYZ','123'

I tried tr '\n' "'\,\'" but no luck


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-05-2016 at 05:39 PM.. Reason: Added CODE tags.
# 2  
Old 10-05-2016
No surprise; tr replaces character by character (search string vs. replacement string)
Try
Code:
sed -n "1h;1!H; $ {x; s/^/'/; s/$/'/; s/\n/','/g; p}" file
'ABC','XYZ','123'

You may need to reset/unset the -H (history expansion) option in your shell.
# 3  
Old 10-05-2016
Try:
Code:
awk '{$1=$1; print q $0 q}' q=\' RS= OFS="','"  file

# 4  
Old 10-06-2016
Or a way in ksh/bash:-
Code:
sep=""                                  # Initially, no separator
while read line
do
   printf "%s'%s'" "$sep" "$line"       # Build up the output (note no new-line)
   sep=","                              # Sets the separator for subsequent lines
done < input_file
printf "\n"                             # Just to neatly start a new line

Probably not the best though, but it might be better if it's easier to understand (and therefore support) than awk or sed options above.

File processing tools like awk & sed are probably much better overall, but they can be a bit of a beast to learn. Sadly, I'm still struggling Smilie


Robin
# 5  
Old 10-06-2016
Hi,

Another version in sed:
Code:
sed ':a;N;$!ba;s/^/'"'"'/;s/\n/'"'"','"'"'/g;s/$/'"'"'/' file

red ones are single quotes.

gives output:
Quote:
'ABC','XYZ','PQR'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search ad replace using begin and end of the file

Hello Friends , Please help to create script for compare and replace if not matches of set of lines . * Primary* Servername Server1 Location R201 Rack 4 *End Primary* *Secondary* Server Name Server1 IPAddress 10.24.30.10 Application Apache *End of Secondary* Above... (4 Replies)
Discussion started by: rnary
4 Replies

2. Shell Programming and Scripting

Replace string and append Indicator to end of record in Linux

Hi All, Could you please help me to achieve below solution. I have a FILE1.txt as below.TEXAS CALIFORNIA TEXAS DALLAS CALIFORNIA CALIFORNIA DALLAS DALLAS TEXAS TEXAS DALLAS NEW YORK NEW YORk FILE2.txt as below.TEXAS,TX DALLAS,DA Now I need to compare the string in FILE2.txt... (6 Replies)
Discussion started by: ureddy
6 Replies

3. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

4. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

5. UNIX for Dummies Questions & Answers

Replace backslash at the end of the string using sed command

I have text file which is a tab delimited one. Sample data from the file is shown below: unix is\ great\ os linux\ is superb I want to replace that backslash with empty string preserving the tab delimiter. Output should be unix is great os linux is ... (3 Replies)
Discussion started by: p.akhilreddy4u
3 Replies

6. Shell Programming and Scripting

How to replace ^M present at the end of each line in a file.

Hi all, My unix admin copied my code from old server to new server. I don't know exactly how he copied by at the end of each line i see ^M appeneded. The code has more than 200 lines could you please help me how to remove it easilt instead of deleting it manually...... Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

7. Shell Programming and Scripting

Replace end of line with a space

for eg: i have i/p file as: ================ i wnt to change end of line ================= my require ouput is like: i wnt to change end of line ==================== (7 Replies)
Discussion started by: RahulJoshi
7 Replies

8. Shell Programming and Scripting

sed : replace space and end-of-line

Hi ! I'm rather new with sed ... learned a lot already by googling etc ... The following script should replace all spaces and ends-of-lines with "something (see below). #!/bin/bash i=0 while read line do fam="H`printf "%06d" $i`" echo $line | sed -e 's//\t'$fam'\n/g' i=$(($i+1))... (7 Replies)
Discussion started by: jossojjos
7 Replies

9. Shell Programming and Scripting

Add end of char \n on end of file

Hi, I want to add \n as a EOF at the end of file if it does't exist in a single command. How to do this? when I use command echo "1\n" > a.txt and od -c a.txt 0000000 1 \n \n 0000003 How does it differentiate \n and eof in this case? Regards, Venkat (1 Reply)
Discussion started by: svenkatareddy
1 Replies

10. Shell Programming and Scripting

How do you search and replace a text with markerA that end with markerB

Hi all, How do you replace all lines that begin with markerA and end with markerB? Ex: I have a file with a list of lines. /sbin/fsck -> $(SEC_CRIT) ; /sbin/fsck.ext2 -> $(SEC_CRIT) ; How can I replace all "->$....;" with a blank space... (1 Reply)
Discussion started by: drone
1 Replies
Login or Register to Ask a Question