Combining sed actions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining sed actions
# 1  
Old 07-28-2014
Combining sed actions

Let's say I have an input file looking like:
Code:
ID1
1    5
6    8
ID2
1    4
5    7

I'm trying to formulate a loop that can combine these actions:
- If the line begins with a letter: replace the '\ n' after a field containing characters with a '\ t' (sed 's / \ n / \ t / g' )
- If the line begins with a number: replace every '\ t' and '\ n' with a comma (sed 's / \ t /, / g; s / \ n /, / g ') EXCEPT if the next line begins with a letter (sed 's / \ t /, / g')


Expected output:
Code:
ID1     1, 5, 6, 8
ID2     1, 4, 5, 7

This is what I tried so far (but I have no idea of how I could format it properly) :
Code:
sed '$1 ~ /^[A-Z]|^[1-9]/ {if ~ /^[A-Z] 's/\n/\t/g'; else if ~ /^[1-9] 's/\n/,/g'}'

Any help would be appreciated Smilie Thanks in advance
# 2  
Old 07-28-2014
sed does not have if...else constructs. You could use awk for this. With sed and exactly the sample structure that you supplied, this might work:
Code:
sed '/^[[:alpha:]]/ {N;N;s/\n/\t/g;s/\t/, /g;s/,/\t/}' file
ID1     1, 5, 6, 8
ID2     1, 4, 5, 7

# 3  
Old 07-28-2014
Hi,
Another sed solution (general):
Code:
$ cat iid.txt
ID1
1       5
6       8
ID2
1       4
5       7
5       7
5       7

ID3
1       4
$ sed  -e ':yy;/^[A-Z]/{s/$/\t/;:xx;$bzz;N;s/\(\n\|\t\)\([0-9]\)/,\2/g;txx;};h;s/.*\n//;x;s/,//;s/\n.*//p;x;tyy;:zz;s/,//' iid.txt
ID1     1,5,6,8
ID2     1,4,5,7,5,7,5,7

ID3     1,4

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help using combining variables with sed command (RHEL 7)

Here is the whole script, very simple, but I am just learning ROK_NO=$1 RPT=/tmp/test sed -E '/^SELECT/ s/(.{23}).{8}/\1'"$ROK_NO"' /' $RPT echo $RPT When I run this I get $ bash rok.sh 2388085 : No such file or directory /tmp/test When I type the command in console, it works... (3 Replies)
Discussion started by: isey78
3 Replies

2. Shell Programming and Scripting

combining multiple sed statements

I need to run a cronjob that will monitor a directory for files with a certain extension, when one appears I then need to run the below scripts How do I go about combining the following sed statements into one script? and also retain the original filename.? sed 's/71502FSC1206/\n&/g' # add a... (2 Replies)
Discussion started by: firefox2k2
2 Replies

3. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

4. Shell Programming and Scripting

combining awk and sed

Hi experts, I have a requirement, In which I need to display the first and last line of a zip file where the line starts with "L". I've writen the code like below using sed and awk. gunzip -c 20110203.1104.gz | awk '$1 ~ "^L" {print substr($0,178,15)}' | sed -n '1p;$p' Is it possible to do it... (8 Replies)
Discussion started by: senthil.ak
8 Replies

5. Shell Programming and Scripting

Need help on Mulitple files mutliple actions

Hi all, I have mistkanely gzipped twice an entire folder and sub folders, and also renamed the files during that process. I am trying to undo this, and I need help to create the batch to work on it. All folders are under my images directory, I have a output.txt file that holds all the... (1 Reply)
Discussion started by: saariko
1 Replies

6. Shell Programming and Scripting

Sed: Combining Multiple Lines into one

Before I ask my actual question, is it going to be a problem that I want to run this process on a 15 Gig file that is ~140 million rows? What I'm trying to do: I have a file that looks like Color,Type,Count,Day Yellow,Full 5 Tuesday Green,Half 6 Wednesday Purple,Half 8 Tuesday ...... (3 Replies)
Discussion started by: goldfish
3 Replies

7. UNIX for Dummies Questions & Answers

combining sed commands

I would like to change the lines: originalline1 originalline2 to: originalline1new originalline1newline originalline2new originalline2newline To do this, id like to combine the commands: sed 's/^/&new/g' file > newfile1 and sed '/^/ a\\ newline\\ \\ (2 Replies)
Discussion started by: Dave724001
2 Replies

8. HP-UX

Auditing User's actions

Hi all I hope to find what i'm looking for in this forum as said in the topic i want to track user's actions on the system. i mean also the action of moving or removing files. I have an HP 9000 with HP UX 11i. the users log on the HP from a terminal window under WIndows XP Thx (3 Replies)
Discussion started by: Timberland
3 Replies

9. Shell Programming and Scripting

Help with simple scripting actions

Hi, I am a beginner in unix shell scripting. I wanted simple information like 1- How to know what are the number of command line options given for the script file? 2- How to check if a variable value is interger or string? 3- How to use awk to replace value of a variable For example I... (5 Replies)
Discussion started by: Nads
5 Replies
Login or Register to Ask a Question