0

I try to use the awk command on my file in dpkg and I get that error:

awk  '{print $1,$6}' cmd.txt > cmd2.txt

File content:

Port         Protocol Type              Board Name                FQBN             Core       
/dev/ttyACM0 serial   Serial Port (USB) Unknown                                               
/dev/ttyACM1 serial   Serial Port (USB) Arduino Mega or Mega 2560 arduino:avr:mega arduino:avr
/dev/ttyAMA0 serial   Serial Port       Unknown                                               
/dev/ttyAMA1 serial   Serial Port       Unknown                                               
/dev/ttyAMA2 serial   Serial Port       Unknown                                               
/dev/ttyAMA3 serial   Serial Port       Unknown                                               
/dev/ttyS0   serial   Serial Port       Unknown                                               
/dev/ttyUSB0 serial   Serial Port (USB) Unknown                                               
/dev/ttyUSB1 serial   Serial Port (USB) Unknown                                               
/dev/ttyUSB2 serial   Serial Port (USB) Unknown                                               
/dev/ttyUSB3 serial   Serial Port (USB) Unknown

It seems in dpkg, there are different behaviors. Can someone give me an advice.

Edit: My ideal case

arduino-cli board list | awk -F ' ' '{print $1,$6}' |while read var1 var2
do
  if [ "$var2" == "Arduino" ];then
      device_path=$var1
      dosomething...
      break;
  fi
done
12
  • What do you mean by "use the awk command on my file in dpkg"? Do you mean you're running this in Debian?
    – muru
    Jul 5, 2022 at 12:51
  • I suspect this is a quoting issue and you are doing something equivalent to sh -c "awk '{print $1,$6}' cmd.txt" - in which $1 and $6 are unintentionally being expanded by the shell Jul 5, 2022 at 14:53
  • @muru dpkg is used by Ubuntu as well. And it seems that this user is packaging a .deb package (from another question).
    – Esther
    Jul 5, 2022 at 16:06
  • @Esther interesting. OP: please clarify where exactly you're running this script (if from a Debian postinst script, then a) show us the actual script, and (b) which OS you're installing this package in)
    – muru
    Jul 5, 2022 at 17:07
  • 1
    Again: which OS are you running this on? Please post a command where the error is reproducible, not some "ideal" case.
    – muru
    Jul 6, 2022 at 10:08

1 Answer 1

0

Awk works with record delimiters (i.e. space, coma, semicolon...). Your input file is not with delimiters, is presented as columns formatting. And unfortunately, some data columns (3 and 4) contain spaces.

You could use this

cut -c 1-13,84- cmd.txt > cmd2.txt

or this:

awk  '{print substr($0,1,12) " " substr($0,84)}' cmd.txt > cmd2.txt

The big problem with those solutions, that the length of each column!

But, it's possible to determine the lengths:

Awk script file columns.awk:

#! /usr/bin/awk -f

BEGIN {
    COLS[++NB_COLS] = "Port"
    COLS[++NB_COLS] = "Protocol"
    COLS[++NB_COLS] = "Type"
    COLS[++NB_COLS] = "Board Name"
    COLS[++NB_COLS] = "FQBN"
    COLS[++NB_COLS] = "Core"
}
NR == 1 {
    for (COL in COLS) {
        START_COL[COL] = index($0, COLS[COL])
        if (COL > 1) {
            LEN_COL[COL - 1] = START_COL[COL] - START_COL[COL - 1] - 1
        }
    }
    LEN_COL[NB_COLS]=length($0)
}
{
    print substr($0, START_COL[1], LEN_COL[1]) " " substr($0, START_COL[6], LEN_COL[6])
}

With:

chmod 755 columns.awk

Used like:

./columns.awk cmd.txt > cmd2.txt

cmd2.txt file content:

Port         Core
/dev/ttyACM0 
/dev/ttyACM1 arduino:avr
/dev/ttyAMA0 
/dev/ttyAMA1 
/dev/ttyAMA2 
/dev/ttyAMA3 
/dev/ttyS0   
/dev/ttyUSB0 
/dev/ttyUSB1 
/dev/ttyUSB2 
/dev/ttyUSB3 
1
  • Is there a way to do this without using a file. As I do in my edited post? Jul 6, 2022 at 10:04

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.