Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

swap lines in a file 4

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
Can anyone suggest the best way to swap 2 lines in a file? I want to swap line 14 with line 26.

I assume there must be a quick way of doing this with awk or perl or maybe something else but if so ... I don't know it.

Thanks in advance
 
This should work with awk/nawk - Untested though !
Loads an array with all lines and then swaps later
NB - Any duplicate lines ? if so, don't use this !

awk '
{ var_name[$0]++ }

END {
for (i in var_name)
if(i==14)
printf("%s\n", var_name[26]);
else if(i==26)
printf("%s\n", var_name[14]);
else
printf("%s\n", var_name);
}' < infile_name > outfile_name

HTH
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Hi CHoggarth,

do it with sed:
sed -n '14p' infile_name > outfile_name
sed -n '26p' infile_name >> outfile_name

Toni
 
I'm still struggling. Dickie Bird - I get a &quot;syntax error near line 10&quot; with your script - still trying to figure out what's causing that - but there are many duplicate lines anyway so maybe this won't help?

Toni - I need the entire file but with these 2 lines swapped. Your code gives me the 2 lines but not sure what to do with them next?

Still hoping for a solution ...

Chris
 
Dickie Bird - I'm no longer getting a syntax error - now I'm getting &quot;awk: illegal reference to array var_name record number 65&quot; (there are 65 lines in the file).

However - I can think of a way to do it if I knew how to use environment variables in sed - is it possible to do this at all: sed 's/string/$env_var/'

Obviously this just substitutes &quot;string&quot; with &quot;$env_var&quot; - does anyone know of a way to access the value held by this variable name from within a sed subtitute command?

Thanks, Chris
 
I don't know if you can do it in sed - in awk you'd do :
#/bin/ksh
varname=&quot;Some text&quot;
awk -v var_name=&quot;$varname&quot; '
BEGIN
etc etc

HTH
Dickie Bird
db@dickiebird.freeserve.co.uk
 
Thanks for all the replies. Useful to know the sed answer but I've managed to get this to work:
#!/usr/bin/ksh
awk '
{
# print input line by line
line[NR] = $0 # remember each input line
}
END { # print lines
i = 0
while (i < NR)
{
if (i == 26)
print line[14]
else if(i == 14)
print line[26]
else
print line
i = i + 1
}
}
' inputfile

I'm not much of an awk programmer so can't explain why this works & DB's first suggestion gave me a problem - but it put me on the right lines anyway.

Thanks again.
 
Why don't you use a curde method with head and tail ?
For example :
#!/bin/ksh
i=1
cat logfailed.sql | while read line
do
case $i in
12) head -26 infile | tail -1 >> outfile
i=`expr $i + 1`
;;
13) head -14 infile | tail -1 >> outfile
i=`expr $i + 1`
;;
*) printf &quot;$line\n&quot; >> outfile
i=`expr $i + 1`
;;
esac
done
mv outfile infile

I think it workes.
 
oups !
didn't see there's so many people !
(sorry,i use logfailed.sql for infile)
Bye,

;-)
 
C.H
Nearly right
You missed from the end of this line :-
else
print line <-
i = i + 1

Well done for thinking it through yerself !
Dickie Bird
db@dickiebird.freeserve.co.uk
 
Ooops - no you didn't - open square bracket, lowercase i and close square backet turns on italics ! Dickie Bird
db@dickiebird.freeserve.co.uk
 
DB - Wish I could really take the credit - I reckoned yours must have been close & looked on the web for something similar :) (can't remember where I found it now)

Gillou - good idea, I would have done that if I'd thought of it.

PS - I'm still waiting for my question to appear on Google Groups comp.unix.shell - hooray for Tek-Tips !
 
When memory is not a problem:
Code:
#!/usr/bin/perl -w
@line = <>;
( $line[13], $line[25] ) = ( $line[25], $line[13] );
print @line;
Cheers, Neil
 
It is amazing how many programmer fall victim to the [ignore][/ignore] bug here at tek-tips. I keep getting after xutopia about this, it seems I may need to write a FAQ.
[ol][li]If you are going to use i as a variable for the array index, please put [ignore]
Code:
 ...
[/ignore]
around your code. It will preserve indenting as well as display all array indecies. The limitation is that you cannot use [ignore]..[/ignore] to bold text or [ignore]...[/ignore] for adding color to your response.[/li]
[li]You can use a different variable than simply i. I was taught to always use variable names with a minimum of 3 characters. So I use inx as my array index. This gives the benefit that if I have to do a search and replace the chances of replacing inx inadvertantly is slim to none. The same would not be true with i.[/li]
[li]Lastly, use the preview button to see what your response will be before you post it. Having an edit feature is one of the strengths of tek-tips, and when posting code it is a necessity.[/li][/ol]

Hope this helps - I'm not trying to stand on a soap-box and toot my own horn, but I want others to share code without confusion. Good luck in your efforts [yoda] Einstein47
(How come we never see the headline, &quot;Psychic Wins Lottery&quot;?)
 
Point taken !
(hangs head in shame) Dickie Bird
db@dickiebird.freeserve.co.uk
 
One trick I didn't see here is worth mentioning,
within a script you can use vi:

vi $file << EOF
:14mo26
:25mo13
:wq
EOF

Quick and simple.
 
quirkasaurus - that is so cool!!!

I have never used the :<line-number>mo<new-line-number> command in vi. Where did you learn that? What other hidden vi tricks do you know? Einstein47
(How come we never see the headline, &quot;Psychic Wins Lottery&quot;?)
 
Einstein:

Here's another vi trick:

# Consider the need to replace all instances of Linux with GNU/Linux....

#

find . -type f -print | xargs file | grep -i text | cut -f1 -d: | while read

file; do

vi $file >/dev/null 2>&1 <<!

:%s,Linux,GNU/Linux,g

:wq

!

done
 
Neat !
Never thought of using vi at the command line to manipulate files - Stars for you guys !
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top