How can I remove first column with awk?

cat input.txt

a x
b y
c z

Expected output

x
y
z

Hello cola,

Could you please try following and let me know if this helps you.
Solution 1st: To get by 2nd field in code.

awk '{print $2}'   Input_file

Solution 2nd: Considering your Input_file has only 2 fields so getting the last field of it then.

awk '{print $NF}'   Input_file

Solution 3rd: Using cut command for this task may help too.

cut -d" " -f2   Input_file

Thanks,
R. Singh

Another solution in awk. Just empty the first field and print the line

awk '{$1=""}1' input.txt
1 Like

Try also

colrm 1 2 <file
x
y
z
1 Like

$1 is the first column, first column is replaced by null string. But what is 1 here?

The one is a boolean (truth) value. It means print this line. So the command removes the first field then prints what is left.

Is it possible to rewrite this code without 1? (using something like print)

You perhaps have other options:-

sed 's/^.* //' input.txt              # Will give you just the last field, so I'm assuming you only have two.
cut -f2- -d" " input.txt              # Will give you all the columns except the first

perl -e 'foreach (<>) {
   @line=split / /;
   shift @line;
   print join (" ",@line)
}' < input.txt                        # Probably a bit long though (and I'm sure someone has a neater way, I'm new to Perl and just practising really)

More importantly, what do you want to do with the data? I'm assuming that this is not your real data and that you have a purpose in mind.

Is this part of a larger shell script perhaps? If you show us the code so far, we can help you work out a suitable solution.

Kind regards,
Robin

1 Like
while read a b
do
   echo "$b"
done < infile

Sure, the 1 is geek for {print} .

awk '{$1=""}{print}' input.txt

or

awk '{$1=""; print}' input.txt

$2="" would clear the 2nd column.

The following sed works similar but does not leave additional spaces

sed 's/[[:space:]]*[^[:space:]]\{1,\}[[:space:]]*//1' input.txt

//2 would be the 2nd column.

After deleting the first column there a leading space. How can I delete the first column with the extra space between first and second column?

If you don't like ALL the other proposals in this thread, try

awk 'sub("^" $1 FS, _)' file
x
y
z