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

aaaaaaaaaa
bbbbbbbbb
cccccccc
aaaaaaaaaa
bbbbbbbbb
cccccccc
aaaaaaaaaa
cccccccc
bbbbbbbbb
aaaaaaaaaa
cccccccc
bbbbbbbbb

Is this what you mean:

awk '(NR%3)==2{p=$0; getline; $0=$0 RS p}1' infile

output:

aaaaaaaaaa
cccccccc
bbbbbbbbb
aaaaaaaaaa
cccccccc
bbbbbbbbb
1 Like

I'd first split the file after every 3 lines, like so:

split -l3 file

In this case 2 files are produced: xaa xab

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

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:

#!/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:

% ./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

1 Like
sed -n 'p;n;h;n;p;g;p'
1 Like

There is always a solution to this the forum:b:

Thanks to everyone

Nice...

1 Like

Could you please explain me how does it works.

sed starts reading on line 1, the -n option suppresses automatic printing.

p: print the current (first) line
n: read the second line
h: do not print that line but put it into hold space
n: read the third line
p: print the third line
g: now get the second line that was stored in hold space
p: print that second line

After that sed starts reading line 4 and the cycle repeats

S.

2 Likes