awk - ORS refuse to separate output record to new line -


hello guys have file:

paumic@os:~/l26$ cat p1 cat dog cow cat dog mouse dog cow cat 

so want first 2 columns , have them this:

cat dog cat dog dog cow 

when use script is:

paumic@os:~/l26$ cat skript #!/bin/sh     output=$(cat $1 | awk 'begin{ors="\n"} {print $1, $2}' ) echo $output 

i answer:

paumic@os:~/l26$ ./skript p1 cat dog cat dog dog cow 

if use else instead of \n works. example:

paumic@os:~/l26$ cat skript #!/bin/sh output=$(cat $1 | awk 'begin{ors="()"} {print $1, $2}' ) echo $output  paumic@os:~/l26$ ./skript p1 cat dog()cat dog()dog cow() paumic@os:~/l26$ 

thank you time , help.

always quote variables. may not required, it's safer habit in.

also, can streamline use of awk , include error handling missing options implicitly:

output=$(awk 'begin{ors="\n"} {print $1 ors $2}' "${1:?no file provided}" ) echo "$output" 

though better explicit error handling:

#!/bin/sh  if [ -z "$1" ];   echo "error: no file specified" >&2   exit 1 elif [ ! -f "$1" ];   echo "error: no such file '$1'" >&2   exit 1 elif [ ! -r "$1" ];   echo "error: file unreadable: '$1'" >&2   exit 1 elif [ ! -s "$1" ];   echo "warning: file empty: '$1'" >&2 fi  output=$(awk -v ors="\n" '{print $1,$2}' "$1" )  echo "$output" 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -