Переместить первый столбец в файле в последний столбец

Команда shar, см. man shar, была придумана именно для этого.

Из man sharилиhttps://manpages.ubuntu.com/manpages/bionic/en/man1/shar.1.html

ОПИСАНИЕ

   shar creates "shell archives" (or shar files) which are in text format and can be emailed.
   These  files  may be unpacked later by executing them with /bin/sh.  The resulting archive
   is sent to standard out unless the -o option is given.  A wide range of  features  provide
   extensive flexibility in manufacturing shars and in specifying shar "smartness".  Archives
   may be fairly simple (--vanilla-operation) or essentially a mailable tar archive.

и

ПРИМЕРЫ

   The first shows how to make a shell archive out of all C program sources.  The second
   produces a shell archive with all.c and.h files, which unpacks silently.  The third
   gives a shell archive of all uuencoded.arc files, into numbered files starting from
   arc.sh.01.  The last example gives a shell archive which will use only the file names at
   unpack time.

       shar *.c > cprog.shar
       shar -Q *.[ch] > cprog.shar
       shar -B -l28 -oarc.sh *.arc
       shar -f /lcl/src/u*.c > u.sh

2
02.01.2021, 13:14
4 ответа

Вы можете удалить ложный символ CR перед фильтрацией и добавить его позже:

<file.txt tr -d \\015 | awk -F ',' '{for (field=2; field <=NF; field++) { printf "%s", $field FS } print $1}' | sed 's/$/\o015/' > file-new.txt

Например:

# cat | sed 's/$/\o015/' > test.txt
1,2,3,4,5,6,7
a,b,c,d,e,f,g,h
A,B,C,D
0

Ctrl+D

# <test.txt tr -d \\015 | awk -F ',' '{for (field=2; field <=NF; field++) { printf "%s", $field FS } print $1}' | sed 's/$/\o015/'
2,3,4,5,6,7,1
b,c,d,e,f,g,h,a
B,C,D,A
0
1
18.03.2021, 22:39

GNU sed в расширенном режиме регулярных выражений-E:

$ sed -Ee 's/^([^,]*),(.*)(.)$/\2,\1\3/' file | cat -A
2,3,4,1^M$
7,8,9,6^M$
b,c,d,a^M$

С АВК:

$ awk -F ',' '
  BEGIN { OFS = FS }
  NF > 1 {
    first = $1
    cr = substr($NF, length($NF))
    sub(/.$/, "", $NF)
    for (i=1; i<NF; i++)
      $(i) = $(i+1)
    $(NF) = first cr
  }
  1
' file

С жемчугом:

$ perl -F, -pale '
    BEGIN { $" = ","; }         # set OFS to comma
    next unless /,/;            # skip non interesting lines 
    chop $F[-1];                # remove last char, the carriage return
    splice @F, @F, 1, shift @F; # move 1st element to the end
    s/.*/@F\r/;
' file 
0
18.03.2021, 22:39

ЕСЛИ мы можем/хотим видеть входной файл в виде csv-таблицы , вы можете запустить что-то вроде

csvcut -c '2:,1' input

Этот инструмент (csvkit )просмотрите ввод в виде однородной таблицы :пример ввода

a,b,c,d,e
1,2,3,4,5
3,4
6,7,8,9,0

выход:

b,c,d,e,a
2,3,4,5,1
4,,,,3              //implicitly added some empty fields
7,8,9,0,6
1
18.03.2021, 22:39

С GNU awk для мульти -char RS:

$ printf 'foo,bar,etc\r\n' |
awk 'BEGIN{RS=ORS="\r\n"; FS=OFS=","} {$(NF+1)=$1; sub("[^"FS"]*"FS,"")} 1'
bar,etc,foo

или с любым awk:

$ printf 'foo,bar,etc\r\n' |
awk 'BEGIN{ORS="\r\n"; FS=OFS=","} {sub(/\r$/,""); $(NF+1)=$1; sub("[^"FS"]*"FS,"")} 1'
bar,etc,foo
2
18.03.2021, 22:39

Теги

Похожие вопросы