Change order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change order
# 1  
Old 07-05-2010
Change order

Good evening
I have a file as below and want to change the order, as in the second column, sed awk Pearl
Thanks

Code:
aaaaaaaaaa
bbbbbbbbb
cccccccc
aaaaaaaaaa
bbbbbbbbb
cccccccc

Code:
aaaaaaaaaa
cccccccc
bbbbbbbbb
aaaaaaaaaa
cccccccc
bbbbbbbbb

# 2  
Old 07-05-2010
Is this what you mean:
Code:
awk '(NR%3)==2{p=$0; getline; $0=$0 RS p}1' infile

output:
Code:
aaaaaaaaaa
cccccccc
bbbbbbbbb
aaaaaaaaaa
cccccccc
bbbbbbbbb


Last edited by Scrutinizer; 07-05-2010 at 07:40 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-05-2010
I'd first split the file after every 3 lines, like so:
Code:
split -l3 file

In this case 2 files are produced: xaa xab

Code:
for i in x*; do
awk 'NR == 1 { l1=$0 }
     NR == 2 { l2=$0 }
     NR == 3 { print l1; print $0; print l2}' "$i"
done

This User Gave Thanks to pseudocoder For This Post:
# 4  
Old 07-05-2010
Hi.

Similar to that of Scrutinizer, but more explicit, and slightly less efficient (more division operations involving NR), but still a single pass over the data:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate reversal of lines 2 and 3 of trios.

# Infrastructure details, environment, commands for forum posts. 
# Uncomment export command to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p printf awk
set -o nounset
pe

FILE=${1-data1}

# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"

pl " Results:"
awk '
(NR % 3) == 1 { print ; next }
(NR % 3) == 2 { hold = $0; next }
              { print ; print hold }
' $FILE |
tee t1

# Check results.

pl " Comparison with desired results:"
if [ ! -f expected-output.txt -o ! -s expected-output.txt ]
then
  pe " Comparison file \"expected-output.txt\" zero-length or missing."
  exit
fi
if cmp expected-output.txt t1
then
  pe " Passed -- files have same content."
else
  pe " Failed -- files not identical -- detailed comparison follows."
  if diff -b expected-output.txt t1
  then
    pe " Passed by ignoring whitespace differences."
  fi
fi

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
printf - is a shell builtin [bash]
GNU Awk 3.1.5

 || start [ first:middle:last ]
Whole: 5:0:5 of 6 lines in file "data1"
aaaaaaaaaa
bbbbbbbbb
cccccccc
aaaaaaaaaa
bbbbbbbbb
cccccccc
 || end

-----
 Results:
aaaaaaaaaa
cccccccc
bbbbbbbbb
aaaaaaaaaa
cccccccc
bbbbbbbbb

-----
 Comparison with desired results:
 Passed -- files have same content.

cheers, drl
This User Gave Thanks to drl For This Post:
# 5  
Old 07-06-2010
Code:
sed -n 'p;n;h;n;p;g;p'

This User Gave Thanks to alister For This Post:
# 6  
Old 07-06-2010
There is always a solution to this the forumSmilie

Thanks to everyone
# 7  
Old 07-07-2010
Quote:
Originally Posted by alister
Code:
sed -n 'p;n;h;n;p;g;p'

Nice...
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change row by order nr?

Hello, I have a file with thousands of rows and I need to change sequence of lines. Sample file: #NAME #SERVICE 112233 #DESCRIPTION AABBCCDD #SERVICE 738292 #DESCRIPTION FFYYRRTT ... ... ... Desired output: #NAME (5 Replies)
Discussion started by: baris35
5 Replies

2. Shell Programming and Scripting

Change of fields order inline

Hi everyone, What is the best solution to check every line in the xml file and change order of found field along with its value without touching value. Pattern will be given i.e. one line can look like this one: <widget position="value,value" size="value,value" name="value"... (5 Replies)
Discussion started by: TiedCone
5 Replies

3. Shell Programming and Scripting

awk - Why does output order change?

I have this END section in an awk script: END { for (j in littlebin) { print (j, int(log(j)/log(2)) , littlebin) lbsum+=littlebin } for (i in bins) ... (6 Replies)
Discussion started by: treesloth
6 Replies

4. UNIX for Dummies Questions & Answers

change service order

hi guys I have a service that depends on some shares (NFS shares ) that need to be mounted before before the service start so the service-app finds the NFS shares and starts correctly... I am confused here this is what I found but I am not sure what to do in order to change it BTW is Suse... (2 Replies)
Discussion started by: karlochacon
2 Replies

5. Shell Programming and Scripting

change order

I have inside a file 22 25 80 111 631 694 861 875 I need this in the form using awk or sed 22,25,80,111,631,694,861,875 (4 Replies)
Discussion started by: anil510
4 Replies

6. Shell Programming and Scripting

Perl- Nested 'for' order change

Hello, I'm quite new to perl so my question is rather basic and I know there is probably a simple way around it but I can't seem to find it. I have a medium-length code and there is a part that works with a nested for loop: foreach my $j(@primpiddelta){ for (my $k=1;... (0 Replies)
Discussion started by: acsg
0 Replies

7. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

8. AIX

How to change the order of authentication in aix?

I have an AIX system configured as NIS client. I have an local user on the system called "batman" and i have a user by the same name in NIS as well. Now when i try to login with Batman user, the local batman gets in. How do I tell the AIX machine to authenticate Batman as NIS user? ... (2 Replies)
Discussion started by: balaji_prk
2 Replies

9. UNIX for Dummies Questions & Answers

How to change the order of a string ?

Hi , I want to change the order of a string using sed command . Is it possible ? $echo "abc123xyz" | sed 's/\()*\) \(*\)/\2\1/' abc123xyz $ echo "abc123xyz" |sed 's/\()*\) \(*\) \()*\)/\2\1\3/' abc123xyz I want to change the string , abc123xyz as xyz123abc . Is it... (5 Replies)
Discussion started by: rajavu
5 Replies

10. UNIX for Dummies Questions & Answers

change order of fields in header record

Hello, after 9 months of archiving 1000 files, now, i need to change the order of fields in the header record. some very large, space padded files. HEADERCAS05212008D0210DOMEST01(spacepadded to record length 210) must now be 05212008HEADERCASD0210DOMEST01(spacepadded to record length 210) ... (1 Reply)
Discussion started by: JohnMario
1 Replies
Login or Register to Ask a Question