Register
It is currently Sun Apr 22, 2018 9:48 pm

Help with Bash script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Apr 06, 2017 11:32 am   

Joined: Thu Apr 06, 2017 11:24 am
Posts: 2
Hey everybody.
I am quite new to bash so please be pacient with me ;)

i try to write a script which edits a textfile for me.
in the textfile there are adresses writen, howewer they are writen like this:
"name, street, postcode, town,"
and i would like to have it like this

name
street
postcode
town

so the comma removed and each invormation in a new line
thanks in advanced
freshbash


Top
 Profile  
 PostPosted: Tue Jun 06, 2017 1:56 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 273
Edit: I see this thread was a duplicate, and you got a much more complete answer from Watael in the other thread :)

Hello freebash! If you're still looking for a solution, sed is probably a good place to start. You could use sed to search and replace the commas with newlines.

OR you could look into awk. You could set the comma as the field separator, read in each line of input, and put newlines in the output.

I know it's an old thread, but I hate seeing questions without answers. I hope this helps somebody!


Top
 Profile YIM  
 PostPosted: Tue Jun 06, 2017 4:15 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Code:
# sed
# Substitute / ,  /  (with a)  newline / Globally (each occurrence) and then Print
sed -rn 's/,/\n/gp'  yourFile

# cat file to pipe and TRanslate each , into \n
cat yourFile | tr ',' '\n'

# we set the RecordSeparator to , an just print the lines.
# the default OutputRecordSeparator is still a newline.
# a kind of implicit conversion.
awk -v "RS=," '{print}' yourFile

# doing it with bash alone
while read line; do   # we read line for line into a var called "line"
    IFS="," array=( $line )  # set the InternalFieldSeparator of bash to a "," and split the line into an array
    for a in ${array[@]} ; do  # we loop over the array
         echo ${a}  # and print each entry
     done   # values now in array are deleted implicitly by next assingment in line IFS.....
done < <(cat yourFile)   # we read the file with process substitution.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Exabot [Bot] and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP