I have a text file separated by space as follows. I need to re-arrange the columns so that the first column is at the end of each line.

I have an idea of how this could be done using cut -d' ' f1 but I'm wondering if there is an easy way with awk or sed.

Text File:

™️       trade mark
ℹ️       information
↔️..↙️    left-right arrow..down-left arrow
↩️..↪️    right arrow curving left..left arrow curving right
⌚..⌛    watch..hourglass done
⌨️       keyboard
⏏️       eject button
⏩..⏳    fast-forward button..hourglass not done
⏸️..⏺️    pause button..record button
Ⓜ️       circled M
▪️..▫️    black small square..white small square
▶️       play button
◀️       reverse button

Want symbol list to follow description instead.


Use sed

sed 's/\([^ ]*\) *\(.*\)/\2 \1/' infile

This \([^ ]*\) will match everything until a non-space characters seen.
The parentheses \(...\) is used to made a group matching which its index would be \1.

The \(.*\) matches everything after first group and it's indexed \2.
The * in \(...\) *\(...\) out of matched groups will ignore to print in output which is matching spaces between group 1 and 2, you could use \s* (with GNU sed) or [[:space:]]* (standardly) instead to match any spacing characters instead of just ASCII SPC as well.

Then at the end first we are printing matched group2 than group1 with a space between.

awk '{first = $1; $1=""; print $0, first}' file.txt

Store the first column in a variable, write blank to the first column, print the whole line ($0$ is the whole line, now with the first column blanked) followed by the first column.

  • I had to move the last column to the beginning of the file and this worked perfectly. my code was awk '{last = $32; $32=""; print last,$0}' oldfile.txt > newfile.txt – pietro Dec 27 '19 at 11:08
awk '{ printf "%s ",$NF;for (i=2;i<=NF-1;i++) { printf " %s",$i } printf "\n" }' filename

With awk, $NF will signify the last piece of data. Print this and then loop through the other fields from 2 to one but the last field, printing those.

  • 2
    Your answer could probably use a bit more explanation. It's quite a long, "complex" awk, and many people aren't that comfortable with awk. A bit more explanation would help non-awkers – Centimane Aug 21 '17 at 19:16

With Perl:

perl -pe 's/(\S+)(\s+)(.*)/$3$2$1/' file

(\S+) captures all non-whitespace character from the beginning

(\s+) capture all whitespace following the captured characters

(.*) capture everything else until the end of the line

$3$2$1 reverses the 3 matched patterns

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.