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
I'm still struggling. Dickie Bird - I get a "syntax error near line 10" 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?
Dickie Bird - I'm no longer getting a syntax error - now I'm getting "awk: illegal reference to array var_name record number 65" (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 "string" with "$env_var" - does anyone know of a way to access the value held by this variable name from within a sed subtitute command?
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.
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 "$line\n" >> outfile
i=`expr $i + 1`
;;
esac
done
mv outfile infile
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 !
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 Einstein47 (How come we never see the headline, "Psychic Wins Lottery"?)
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, "Psychic Wins Lottery"?)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.