0

I have the following lines:

field1    1-23_4_5    field3
field1    2-40_5_7    field3
field1    3-43_7_9    field3
             .
             .
             .  

I would like to modify the second field so that

1-23_4_5 becomes 1-23

I am thinking the combination of awk and cut would do the trick. Is there a simple way to go about this?

Thank you so much for your help in advance.

3 Answers 3

2

Through awk,

$ awk 'sub(/_[^_]*_[^_]*$/,"",$2)1' file
field1 1-23 field3
field1 2-40 field3
field1 3-43 field3

Through sed,

$ sed 's/^\([^ ]\+\ *[^_]\+\)[^ ]\+/\1/g' file
field1    1-23    field3
field1    2-40    field3
field1    3-43    field3
Sign up to request clarification or add additional context in comments.

Comments

0

Here is one awk version:

awk '{split($2,a,"_");$2=a[1]}1' file
field1 1-23 field3
field1 2-40 field3
field1 3-43 field3

Divide field 2 by _ and use only first part.
PS it changes the formatting. (normal with awk)

Comments

0

You can do this quite easily with a simple bash script using read and substring extraction:

#!/bin/bash

## validate input file
[ -r "$1" ] || { printf "Error: insufficient input. Usage %s <filename>\n"; exit 1; }

## read and extract substring of field2
while read -r f1 f2 f3 || [ -n "$f3" ]; do 
    printf "%s    %s    %s\n" "$f1" "${f2%%_*}" "$f3"; 
done < "$1"

test file:

$ cat dat/field.txt
field1    1-23_4_5    field3
field1    2-40_5_7    field3
field1    3-43_7_9    field3

output:

$ bash fields.sh dat/field.txt
field1    1-23    field3
field1    2-40    field3
field1    3-43    field3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.