Multiple sed in one command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple sed in one command
# 1  
Old 06-23-2011
Multiple sed in one command

Hi,

How do I run multiple sed on a file similar to the below in one command?
I can't global substitute the 401 as it can occur elsewhere in the row and needs to
be maintained if it does.
Code:
sed  "s/,40128/,128/" test2.csv > test3.csv
sed  "s/,40192/,192/" test3.csv > test4.csv
sed  "s/,40194/,194/" test4.csv > test5.csv
sed  "s/,40199/,199/" test5.csv > test6.csv
sed  "s/,40204/,204/" test6.csv > test7.csv
sed  "s/,40211/,211/" test7.csv > test8.csv

Thanks
Kieran

Last edited by Franklin52; 06-23-2011 at 07:29 AM.. Reason: Please use code tags, thank you
# 2  
Old 06-23-2011
how about:
Code:
sed 's/40\([0-9]*\)/\1/g' test.csv

# 3  
Old 06-23-2011
Thanks for the reply. That seems to replace every occurrence of 40 in the row.
I want to change the 40128 to 128 like it does but leave the 401 or 402 (or 403 or 404)

Code:
|402|40128|1|1
|401|40128|1|1

Code:
2|128|1|1
1|128|1|1|


Last edited by Franklin52; 06-23-2011 at 07:30 AM.. Reason: Code tags
# 4  
Old 06-23-2011
Can you post few more sample input data and the output format expected..?
If yours is GNU Sed then try (works for above input file format)
Code:
sed 's/40\([0-9]*\)/\1/2' test.csv

# 5  
Old 06-23-2011
Sure

Input:
|402|40128|1|1 |401|40128|1|1
|403|40199|1|1 |404|40211|1|1

Output required:
|402|128|1|1 |401|128|1|1
|403|199|1|1 |404|211|1|1

Thanks
# 6  
Old 06-23-2011
Code:
awk -F \| '{for (i=1;i<=NF;i++) if (length($i)>3&&$i~/^40/) sub(/40/,"",$i)}1' OFS=\| infile

Code:
sed 's/40\([0-9]\{3\}\)/\1/g' infile

# 7  
Old 06-23-2011
Works a treat..

Thanks for all the replies
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace one value which occurs multiple times

Hi, My Input File : "MN.1.2.1.2.14.1.1" := "MN_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) "MOS.1.2.1.2.13.6.2" := "MOS_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) Like above template,I have... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

2. Shell Programming and Scripting

sed command to replace multiple column in one go

Hi, I want to replace the value in more than one column. For one column ,following command is working - sed 's/./$value_to_replace/$column number' file_name e.g. suppose this is input 1111000000 command - sed 's/./M/5' output= 1111M000000 For two column also command is like - cat... (22 Replies)
Discussion started by: Preeti Chandra
22 Replies

3. Shell Programming and Scripting

sed Command on Multiple Files and retaining the same file name

Hi All I am trying to run sed command to remove first 2 charcaters from a file on Multiple Files in my directory and what to retain the same file name . I want to know how to retain the same file name but with changes . Can some one please let me know how to proceed with this . ... (7 Replies)
Discussion started by: honey26
7 Replies

4. Shell Programming and Scripting

sed remove multiple set of lines in one command

is there a way with sed to removed more than one set of lines in one line? so i mean sed ${firstElem},${lastIndex}d web.xml > web1.xml this will delete lines between ${firstElem},${lastIndex} i want in the same line to do somethinkg like this (doesn't work so far) sed... (3 Replies)
Discussion started by: Poki
3 Replies

5. Shell Programming and Scripting

SED command using multiple input files

What is the syntax to use multiple input files in a SED command. i.e. substitute a word with a phrase in every file in a directory. for every file in /usr/include that has the word "date" in the file grep -l '\<date\>' /usr/include/*.h find each occurrence of the word "time" in the file &... (3 Replies)
Discussion started by: sheoguey
3 Replies

6. UNIX for Dummies Questions & Answers

sed command, make multiple lines into one

:confused:Hi, I'm relativley new at unix so am having difficulties at the most basic of areas. I am trying using sed to make multiple lines into one line. For example, i would like: Mary had a little lamb to look like this Maryhadalittlelamb so far i have tried sed... (1 Reply)
Discussion started by: cavanac2
1 Replies

7. Shell Programming and Scripting

how to use multiple files in sed with w command

i have a command like : sed -n 's/^* /&/w even' <file if i want to write to multiple files like sed -n 's/^* /&/w zero two three' < file its not working it is taking "zero two three" as a single file i want to write to 3 seperate files . pls can anyone help me (2 Replies)
Discussion started by: santosh1234
2 Replies

8. UNIX for Dummies Questions & Answers

Sed command over multiple lines in csh??

hi i have a long sed command in a csh script that won't fit on 1 line. how do i break it up correctly over multiple lines? this doesn't seem to work in csh: sed -e s/template/$IP.$NN/ \ -e s/NRG/6/ \ -e s/inputf/$IS.$NN/ \ -e s/SHIFT/10.0/ <template.egsinp > $IP.$NN.inp i get: sed:... (1 Reply)
Discussion started by: tuathan
1 Replies

9. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies

10. Shell Programming and Scripting

Can we give multiple patterns to a sed command???

HI, I want to know can multiple pattern be given inside a sed statement.. Input: aa_bb_cc.Mar-22-2007 cc_dd_ee.Mar-21-2007 aa_1002985_952.xml aa_bb_032207.txt aa_bb_cc_10002878.dat Output: aa_bb_cc cc_dd_ee aa.xml aa_bb.txt aa_bb_cc.dat (6 Replies)
Discussion started by: kumarsaravana_s
6 Replies
Login or Register to Ask a Question