Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 03-29-2011, 10:23 AM   #1
LQ Newbie
Registered: Mar 2011
Posts: 23

Rep: Reputation: 0
bash: concatenate several orders

Is there anyway to simplify following code, or similar, every time you have an input and an output file and have to "sed" different strings, in order to avoid using so many files?

sed 's/&//g' <"$DIR/file" >"$DIR/file1"
tr '\t' '&' <"$DIR/file1" >"$DIR/file2"
sed 's/$/\\\\/g' <"$DIR/file2" >"$DIR/file3"

Last edited by jmvidalvia; 03-29-2011 at 10:42 AM.
Old 03-29-2011, 10:38 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
A single sed line with multiple commands should do the trick:
sed -e 's/&//g' -e 's/\t/\&/' -e 's/$/\\\\/g' file
moreover sed offers the -i option to edit the file in place. Use it with caution, until you're sure of the results or add a suffix to make a backup copy of the original file, e.g.
sed -i.bck -e 's/&//g' -e 's/\t/\&/' -e 's/$/\\\\/g' file
will create a backup copy called file.bck preserving the original content if soemthing goes wrong.

Another option is process substitution, a shell feature useful to avoid the creation of temporary files. In this case it's easy to use sed as shown, but sometimes it might be handy. For example suppose you have to check the difference between two unsorted files and you want to sort them previously:
sort file1 > file1.sorted
sort file2 > file2.sorted
diff file1.sorted file2.sorted
You can avoid the creation of the sorted files using:
diff <(sort file1) <(sort file2)
Hope this helps.
Old 03-29-2011, 10:38 AM   #3
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You can use pipes, so something like 'sed input | tr | sed > file'. Or you can write a sed script with the '-e' option. EDIT: see above for an example.
Old 03-29-2011, 10:45 AM   #4
LQ Newbie
Registered: Mar 2011
Posts: 23

Original Poster
Rep: Reputation: 0
Great! thanks so much.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script: concatenate variables with period character the182guy Linux - Software 4 12-28-2009 01:41 PM
work orders system alexr186 Linux - Software 6 03-01-2006 07:50 AM
[Bash] Concatenate string using awk senorsnor Programming 7 05-05-2005 12:38 AM
suffix search orders and DHCP badgolfer73 Linux - Newbie 4 05-26-2004 06:59 AM
Bash: How to concatenate the output of different commands? J_Szucs Programming 1 11-13-2003 07:43 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration