Swapping or switching 2 lines using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swapping or switching 2 lines using sed
# 22  
Old 11-16-2008
Quote:
Originally Posted by null7
Sorry if this question is annoying you. Smilie
Not at all, because you're right. I've correct the code, if you have any question don't hestitate to ask.

Code:
#!/bin/sh

line=3
line1=9

awk -v var="$line" -v var1="$line1" 'NR==var {
  s=$0
  for(i=var+1;i<var1;i++){
    getline;s1=s1?s1 "\n" $0:$0
  }
  getline;print;print s1 "\n" s
  next
}1' file

Regards
# 23  
Old 11-16-2008
And another one:

Code:
awk 'NR == l1, NR == l2 {
  _[NR] = $0
  if (NR == l2) {
    print
    for (i=l1+1; i<l2; i++)
      print _[i]
    print _[l1]
    }
  next
  }1' l1=3 l2=9 infile

# 24  
Old 11-18-2008
Quote:
Originally Posted by Franklin52
Not at all, because you're right. I've correct the code, if you have any question don't hestitate to ask.

Code:
#!/bin/sh

line=3
line1=9

awk -v var="$line" -v var1="$line1" 'NR==var {
  s=$0
  for(i=var+1;i<var1;i++){
    getline;s1=s1?s1 "\n" $0:$0
  }
  getline;print;print s1 "\n" s
  next
}1' file

Regards
Thanx and its work. but can u explain me this line:
Code:
s1=s1?s1 "\n" $0:$0

I know its C conditional and the output pattern but didn't get the meaning. I tried print line by line to see the output. What i got was,

1st loop:
Code:
before getline, s1:$0:3
after getline, s1:$0:4
after s1=s1?s1 "\n" $0:$0, 4

2nd loop:
Code:
before getline, s1:4$0:4
after getline, s1:4$0:5
after s1=s1?s1 "\n" $0:$0, 
4
5

3rd loop:
Code:
before getline, s1:
4
5$0:5
 after getline,s1:
4
5$0:6
 after s1=s1?s1 "\n" $0:$0, 
4
5
6

# 25  
Old 11-19-2008
Hi,

Code:
s1=s1?s1 "\n" $0:$0

This is a conditional expression operator. The format is : condition ? expression1 : expression2.
If the condition is true, the result is expression1 else the result is expression2.

With our command we check the value of s1 (s1?). If s1 is true (s1 is not empty) we assign the contents of s1, a newline ("\n") and the current record ($0) to s1. If the condition is false (s1 is empty) we assign the current record ($0) to s1.

Regards
# 26  
Old 11-19-2008
hi

below sed should be ok for you


Code:
sed -n '3 !{
p
}
3 {
h
n
x
H
n
x
H
x
p
}' file

# 27  
Old 11-19-2008
Quote:
Originally Posted by Franklin52
Hi,

Code:
s1=s1?s1 "\n" $0:$0

This is a conditional expression operator. The format is : condition ? expression1 : expression2.
If the condition is true, the result is expression1 else the result is expression2.

With our command we check the value of s1 (s1?). If s1 is true (s1 is not empty) we assign the contents of s1, a newline ("\n") and the current record ($0) to s1. If the condition is false (s1 is empty) we assign the current record ($0) to s1.

Regards
Owh..thank you. you opened my eyes. too many thing is mind, and i didn't realize it. didn't realize "\n" $0 statement is expression1. phewhh..Smilie thanx again.

radoulov/summer_cherry,
Appreciate your feedback but yet to try. Smilie Thanx.

Btw, i don't know whether I can post this question here. Seems it related to this swapping, i think i can post it here. Sorry if not.

Let's say I've files named file1, file2, file3 and so on.
Each file contain the same format.
What I'm working now is i'm trying to swap line 2 and line 3 for each file(success with previous code)
but there is condition where the 2nd line must contains specific string, let say: 1012345678. To simplyfy, the string is in column 4.
If 2nd line of the file does not contain the specified string, the script do nothing.

My Algorithm:
Code:
if (line==2 && column 4 == 1021436587)
{
    backup the original file (yet to get the solution)
    swap the line
}
else (optional)
{
    don't do anything
}

Code:
#!/bin/sh

ls file* > list
for i in `cat list`
do

awk '{ if ((NR==2) && ( $4 == 1012345678))
    {
        nawk -v var="$line" -v var1="$line1" 'NR==var 
        {
            s=$0
            for(i=var+1;i<var1;i++)
            {
                #print "yyyyy " "s1:" s1 "$0:" $0 " yyyyy"
                getline;
                #print "xxxxx " "s1:" s1 "$0:" $0 " xxxxx"
                s1=s1?s1 "\n" $0:$0
                #print "zzzzz " s1 " zzzzz"
            }
                getline;print;print s1 "\n" s
                next
        }1' $i
     } else 
     {
        print "do nothing"
     }
       }' $i
done

Eg input file1:
Code:
header
abc  def   ghi   1012345678       500010.00                       
abc  def   ghi   2022222222             10.00 
abc  def   ghi   1022222222             10.00                       
abc  def   ghi   2012345678       500010.00

output:
Code:
header
abc  def   ghi   2022222222             10.00
abc  def   ghi   1012345678       500010.00                                          
abc  def   ghi   1022222222             10.00
abc  def   ghi   2012345678       500010.00

I have problem to get it right. Hopefully i can get suggestion/advise/solution from you guys. Smilie

Rgrds.
# 28  
Old 11-19-2008
To swap the 2nd and the 3th line:

Code:
awk 'NR==2 && $4 == "1021436587" {
  s=$0;getline;print $0; print s;next
}
{print}' file > outfile

Explanation:

If linenumber = 2 and field 4 is "1021436587":
1. put the contents of the record to the string s.
2. get the next line.
3. print the line
4. print the previous line
5. get the next record.

The next statement reads the next line and the passes controle back to the first line of the script while the getline function goes further with the next command.

Btw, in the output of your example you've swapped the 1st and the 2nd line.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Swapping lines

Hi there, I have a text that I'm trying to format into something more readable. However, I'm stuck in the last step. I've searched and tried things over the internet with no avail. OS: Mac After parsing the original text that I won't put here, I managed to get something like this, but this... (8 Replies)
Discussion started by: Kibou
8 Replies

2. Shell Programming and Scripting

Swapping the 1st 4 lines only

How can you swap the first 4 line only, the rest will stay the same. thanks #!/bin/sh line=4 awk -v var="$line" 'NR==var { s=$0 getline;s=$0"\n"s getline;print;print s next }1' fileko.tx . desired output: (8 Replies)
Discussion started by: invinzin21
8 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

AWK swapping fields on different lines

Hi All, Sorry if this question has been posted elsewhere, but I'm hoping someone can help me! Bit of an AWK newbie here, but I'm learning (slowly!) I'm trying to cobble a script together that will save me time (is there any other kind?), to swap two fields (one containing whitespace), with... (5 Replies)
Discussion started by: Bravestarr
5 Replies

5. Shell Programming and Scripting

Switching lines

Hi I'm quite new with linux. Very simple, I need to swap every 2 lines in a file. Example INPUT: a a a b b b x x x y y y s s s t t t OUTPUT: b b b a a a y y y x x x t t t (5 Replies)
Discussion started by: hernand
5 Replies

6. Shell Programming and Scripting

Swapping three lines

I have some text: <date>some_date</date> <text>some_text</text> <name>some_name<name> and I want to transform it to smthng like that: some_name on some_date: some_text I've tried sed: sed 's/<text>\(.*\)<\/text> <name>\(.*\)<\/name>/\2 - \1/' but it says unterminated... (13 Replies)
Discussion started by: dsjkvf
13 Replies

7. Homework & Coursework Questions

Swapping Fields with Sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The assignment is to convert a text table to csv format. I've got the cleaning up done, but I need to swap two... (0 Replies)
Discussion started by: VoiceInADesert
0 Replies

8. Shell Programming and Scripting

awk: switching lines and concatenating lines?

Hello, I have only recently begun with awk and need to write this: I have an input consisting of a couple of letters, a space and a number followed by various other characters: fiRcQ 9( ) klsRo 9( ) pause fiRcQ 9( ) pause klsRo continue 1 aPLnJ 62( ) fiRcQ continue 5 ... and so on I... (7 Replies)
Discussion started by: Borghal
7 Replies

9. Shell Programming and Scripting

swapping lines that match a condition using sed, perl or the like

I'm a bit new to regex and sed/perl stuff, so I would like to ask for some advice. I have tried several variations of scripts I've found on the net, but can't seem to get them to work out just right. I have a file with the following information... # Host 1 host 45583 { filename... (4 Replies)
Discussion started by: TheBigAmbulance
4 Replies

10. Shell Programming and Scripting

Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this: fixed-address 192.168.6.6 { hardware ethernet 00:22:64:5b:db:b1; host X; } fixed-address 192.168.6.7 { hardware ethernet 00:22:64:5b:db:b3; host Y; } fixed-address 192.168.6.8 { hardware ethernet 00:22:64:5b:db:b4; host A; }... (4 Replies)
Discussion started by: ksk
4 Replies
Login or Register to Ask a Question