How to interchange the lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to interchange the lines
# 1  
Old 11-04-2006
How to interchange the lines

Hi,

I want to know that, how to inter change the lines.
In the following file two line are there first one is conatin PD next line PY.
But i want to first line PY and next line PD.

My file structure is like this:

TI Electronic Publishing
AU Robert
PD NOV 11
PY 2000
AB This is deals with electronic publishing and open access.

TI Digital Library
AU David
PD DEC 12
PY 2006
AB This is deals with electronic publishing and open access.

Let me know how to change the lines.

Shankarao
# 2  
Old 11-05-2006
Try...
Code:
awk '{if ($1=="PD") pd=$0; else if ($1=="PY") print $0 ORS pd; else print $0}' file1

Or in short form...
Code:
awk '$1=="PD" {pd=$0; next} {print} $1=="PY" {print pd}' file1


Last edited by Ygor; 11-05-2006 at 06:19 AM..
# 3  
Old 11-05-2006
exchange of lines.

With related with my earlier mail is tried with following code but its giving errror,

awk '$1=="PD" {pd=$0; next} {print} $1=="PY" {print pd}' file1

let me know how to exchange the lines.

shankarao
# 4  
Old 11-05-2006
open the file and save the result

Hi,
I tried with this command it works fine, Now i want to open the file and store the result in that file.
awk '{if ($1=="PD") pd=$0; else if ($1=="PY") print $0 ORS pd; else print $0}' 2000.txt

Let me know how to open and save the result in that file.

With regards,
Shankarao
# 5  
Old 11-05-2006
You cannot do that with awk (atleast to my knowledge). What you can do instead is this:
Code:
awk '{if ($1=="PD") pd=$0; else if ($1=="PY") print $0 ORS pd; else print $0}' 2000.txt > 2000.txt.tmp; mv 2000.txt.tmp 2000.txt

# 6  
Old 11-05-2006
Code:
sed "/^PD/{N;/\nPY/{s/\(.*\)\n\(.*\)/\2\n\1/;};}" file

Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Interchange the Rows and column

Hi, I have following input and want to change it to following output INPUT 01-APR-14,KB,822714 01-APR-14,MB,8133431 02-APR-14,KB,757140 02-APR-14,MB,7770368 03-APR-14,KB,815427 03-APR-14,MB,7590511 04-APR-14,MB,7529895 04-APR-14,KB,779561 05-APR-14,MB,8151537 05-APR-14,KB,809675 ... (6 Replies)
Discussion started by: siramitsharma
6 Replies

2. Shell Programming and Scripting

UNIX script to interchange column values

Hi, I have the below script which is fetching the data from a file as below: awk -F "~" 'NR>1 {print $5}' 1.txt The o/p is like: 1452350 1458638 1452350 2 5696837 No i want to put 5696837 before 1458638 in above o/p So final o/p should look like: 1452350 5696837 1458638 (2 Replies)
Discussion started by: Vivekit82
2 Replies

3. UNIX for Advanced & Expert Users

How to interchange the places of 1st name and last name?

cat emp.lst 12 |Rob Cliff |G.M. 14 |Mark Rob |Chairman Please use awk to invert the names in the file emp.lst, i.e. the surname should be 1st and then the 1st name. There are trailing spaces even in each field making them fixed length. (4 Replies)
Discussion started by: ravisingh
4 Replies

4. Shell Programming and Scripting

Easy edit problem: interchange columns

hi; my file1.txt: cell137 1 cell337 1 cell355 1 cell355 3 cell360 1 cell360 2 cell360 3 my file2.txt: ... cell137 1 20.64.1.97 cell137 2 20.64.1.97 cell137 3 20.64.1.97 ... cell337 1 20.64.1.113 cell337 2 20.64.1.113 cell337 3 20.64.1.113 (4 Replies)
Discussion started by: gc_sw
4 Replies

5. Shell Programming and Scripting

vi editor - interchange two lines

Hi, I have a quick question regarding vi editor.Is it possible to interchange two lines in vi editor without using the possibilites of cut-paste,copy paste etc? Your help is appreciated! Regards Dileep (4 Replies)
Discussion started by: DILEEP410
4 Replies
Login or Register to Ask a Question