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
# 29  
Old 11-19-2008
A Perl solution of the previous requirement (it will backup all your files, not only those containing the pattern):

Code:
perl -i~ -0777pe'
  s/\A(.*?\n)
   ((?:\S+\s+){3}1012345678\b.*?\n)
   (.*?\n)
   /$1$3$2/xs
  ' file1 file2 ... filen

You won't see any output. Check the files after running the script Smilie.
# 30  
Old 11-20-2008
Quote:
Code:
awk 'NR==2 && $4 == "1021436587" {
s=$0;getline;print $0; print s;next
}
{print}' file > outfile
Actually i got the meaning of this code. but if can i just want to check only line 2 and contain =1021436587. If other file not fulfill the condition then do nothing. no need to make backup or anything. btw the output i gave previously is just expected output. sorry. Smilie

Quote:
Code:
perl -i~ -0777pe'
s/\A(.*?\n)
((?:\S+\s+){3}1012345678\b.*?\n)
(.*?\n)
/$1$3$2/xs
' file1 file2 ... filen
For this code, can you explain it cez i'm not really familliar with perl. But i already tried it. Questions, can i change the symbol ~ to something else maybe words and is it possible if we backup only the changes file?

TQ.
# 31  
Old 11-20-2008
Quote:
Originally Posted by null7
Questions, can i change the symbol ~ to something else maybe words and is it possible if we backup only the changes file?
Yes,
adjust $Bak_ext. The glob argument should point to your files (I used file* as pattern).

Code:
perl -e'BEGIN {
  $Pattern = "1012345678";
  $Bak_ext = ".bak";
  @Files = glob "file*";
  }
for (@Files) {
  $File = $_;
  open FH, $File or die "$File : $!\n";
  while (<FH>) {
    if ($. == 2 and (split)[3] == $Pattern) {
      close FH;
      local @ARGV = ($File);
      local $^I = $Bak_ext;
      local $/ = undef;
      while (<>) {
        s/
         \A(.*?\n)
         ((?:\S+\s+){3}$Pattern\b.*?\n)
         (.*?\n)
         /$1$3$2/xos;
         print;
      }
    }
  }
}'

# 32  
Old 11-21-2008
Quote:
Originally Posted by radoulov
Yes,
adjust $Bak_ext. The glob argument should point to your files (I used file* as pattern).

Code:
perl -e'BEGIN {
  $Pattern = "1012345678";
  $Bak_ext = ".bak";
  @Files = glob "file*";
  }
for (@Files) {
  $File = $_;
  open FH, $File or die "$File : $!\n";
  while (<FH>) {
    if ($. == 2 and (split)[3] == $Pattern) {
      close FH;
      local @ARGV = ($File);
      local $^I = $Bak_ext;
      local $/ = undef;
      while (<>) {
        s/
         \A(.*?\n)
         ((?:\S+\s+){3}$Pattern\b.*?\n)
         (.*?\n)
         /$1$3$2/xos;
         print;
      }
    }
  }
}'

I've tried the code and superb.Smilie. it's work. but i don't know why it won't work when i changed @File to another pattern. Is there anything i need to change also. I change @Files = glob "file*"; to @Files = glob "ABC_DEF*"; .
There are more than 1 ABC_DEF* files (Eg:ABC_DEF13_17112008_112749.txt,ABC_DEF24_17112008_112749.txt,ABC_DEF32_17112008_112749.txt)
Btw, is it possible if u can explain line by line this script. I never use perl before this. Really sorry.

Btw, appreciate if someone can translate this script to awk or other command. Smilie

Rgrds.
# 33  
Old 11-21-2008
Quote:
Originally Posted by null7
I've tried the code and superb.Smilie. it's work. but i don't know why it won't work when i changed @File to another pattern. Is there anything i need to change also. I change @Files = glob "file*"; to @Files = glob "ABC_DEF*"; .
There are more than 1 ABC_DEF* files (Eg:ABC_DEF13_17112008_112749.txt,ABC_DEF24_17112008_112749.txt,ABC_DEF32_17112008_112749.txt)


The code should work fine with the changes you mention.
You say it doesn't work, could you explain what exactly is happening?

Quote:
Btw, is it possible if u can explain line by line this script. I never use perl before this. Really sorry.
Are you sure you that want to understand every part (it seams you prefer to avoid Perl)?


Quote:
Btw, appreciate if someone can translate this script to awk or other command. Smilie

You can try to write a shell script for this task if you don't want to use Perl (?).
If you encounter problems we could try to help.
# 34  
Old 11-22-2008
Quote:
The code should work fine with the changes you mention.
You say it doesn't work, could you explain what exactly is happening?
What i mean is when i run the script, there's no changes to the file (not backup and swap) even the file fulfill the condition. that's weird.


Quote:
Are you sure you that want to understand every part (it seams you prefer to avoid Perl)?
Yes if possible. I'm not trying to avoid but i really don't have much time.I thought it fast to learn by your explanation here. But it's oke if you don't have time to explain it. Smilie


Quote:
You can try to write a shell script for this task if you don't want to use Perl (?).
I already make it before this, but look likes no one feedback the correct one. Smilie
# 35  
Old 11-23-2008
I think i know whats the problem. i tried to print out the line number. it seems the script reads those files as one file. What can we do so that the script can read one file than loop for 2nd file? i try to put unix command but don't know how to pass the file variable to replace @Files. Any advise?

Quote:
ls file*.txt > list
for i in `cat list`
do
Perl script
done
Code:
perl -e'BEGIN {
  $Pattern = "4052010204";
  $Bak_ext = ".bak";
  @Files = glob "file*";
  #print "help $i";
  }
  #print "yihaa";
for (@Files) {
  $File = $_;
  open FH, $File or die "$File : $!\n";
  while (<FH>) {
  print "line $. " ;
    if ($. == 2 and (split)[3] == $Pattern) {
    print "hey this line no $. " ;
     close FH;
     local @ARGV = ($File);
     local $^I = $Bak_ext;
     local $/ = undef;
     while (<>) {
     s/
      \A(.*?\n)
        ((?:\S+\s+){3}$Pattern\b.*?\n)
        (.*?\n)
         /$1$3$2/xos;
         print;
      }
   }
  }
}'

output: Eg: each file contain 17 lines.
Quote:
line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 11 line 12 line 13 line 14 line 15 line 16 line 17 line 18 line 19 line 20 line 21 line 22 line 23 line 24 line 25 line 26 line 27 line 28 line 29 line 30 line 31 line 32 line 33 line 34 line 35 line 36 line 37 line 38 line 39 line 40 line 41 line 42 line 43 line 44 line 45 line 46 line 47 line 48 line 49 line 50 line 51
rgrds.
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