When we use the shell to process files, we often need to remove or add newline characters, the name of the problem is how to quickly replace it?
We have such a file
[email protected] working]# cat 1gd200a16c013493,gd200a16c013494,gd200a16c013495,gd200a16c013497
We need to remove the comma and replace it with a line break.
[email protected] working]# cat 1|sed ' s/,/\n/g ' gd200a16c013493gd200a16c013494gd200a16c013495gd200a16c013497
But do we turn around? Just replace the following line with a comma, and sed will not be used in reverse.
[email protected] working]# cat 1|sed ' s/,/\n/g ' |sed ' s/\n/,/g ' gd200a16c013493gd200a16c013494gd200a16c013495gd200a16c013497
We can use awk to do it, as follows:
Cat 1|sed ' s/,/\n/g ' |awk ' {{printf '%s, ', $ A} ' gd200a16c013493,gd200a16c013494,gd200a16c013495,gd200a16c013497
This can also be handled with TR, but the TE will also need to manually delete the last comma as follows:
[email protected] working]# cat 1|sed ' s/,/\n/g ' |tr-s ' \ n ', ' gd200a16c013493,gd200a16c013494,gd200a16c013495, Gd200a16c013497,[[email protected] Working]#[[email protected] working]# cat 1|sed ' s/,/\n/g ' |tr-t ' \ n ', ' Gd200a16c013493,gd200a16c013494,gd200a16c013495,gd200a16c013497,[[email protected] Working]#[[email protected] working]# cat 1|sed ' s/,/\n/g ' |tr ' \ n ', ' Gd200a16c013493,gd200a16c013494,gd200a16c013495,gd200a16c013497,[[email Protected] working]#
This article is from the "Forand" blog, make sure to keep this source http://853056088.blog.51cto.com/12966870/1952430
Shell replace and remove line breaks