Trim end of nth unix column

I have a table structure:

ous.txt 1452 1793

out.txt 36796 14997

ouw.txt 478 4247

3 columns and many rows.

I want to trim ".txt" - last 4 characters from column # 1 (with awk, sed).

I know that line end aborting was covered here, but I don't know how to access the end of the nth column.

+3


source to share


2 answers


Based on your example input, this would do this:

sed 's/\.txt//' filename

      



If I only wanted to work with the 1st space delimited column I would use awk or just the shell:

while read -r col1 col2 col3; do
    printf "%s %s %s\n" "${col1%.txt}" "$col2" "$col3"
done < filename

      

+3


source


If you want to remove the last 4 characters of column 1:

awk '{sub(/....$/, "", $1)} 1' filename

      



If columns are separated by spaces but not tabs:

sed 's/.... / /' filename

      

+4


source







All Articles