sumit gupta
sumit gupta

Reputation: 3

Get last n characters of one field and complete second field of a string in Linux

I have 2 lines in a file :

MUMBAI,918889986665,POSTPAID,CRBT123,CRBT,SYSTEM,151004,MONTHLY,160201,160302
MUMBAI,912398456781,POSTPAID,SEGP,SEGP30,SMS,151004,MONTHLY,160201,160302

I wanted to cut field 2 and 4 in above lines. Condition is: from field 2, I need only ten digits.

Desired output:

8889986665,CRBT
2398456781,SEGP30

I am trying below command :

 cut -d',' -f2 test.txt | cut -c3-12 && cut -d',' -f4 test.txt

My output:

 8889986665
 2398456781
 CRBT
 SEGP30 

Kindly help me to achieve desired output.

Upvotes: 0

Views: 382

Answers (4)

Preston Garrison
Preston Garrison

Reputation: 103

cat test.txt | cut -f 2,4 -d ","

assuming your file is test.txt

Upvotes: 0

Rahul Gundecha
Rahul Gundecha

Reputation: 33

Solution 2: Here is the solution which will serve the purpose:

cut -d',' -f2,4 1 | sed 's/.*\([0-9]\{10\}\),\(.*\)/\1,\2/'

8889986665,CRBT123

2398456781,SEGP

  • cut will give us the second and forth field.
  • Inside sed, .* to skip the initial characters until the first pattern ahead is encountered.
  • First pattern is 10 digits followed by a semicolon: \([0-9]\{10\}\),
  • Second pattern is rest of the line: \(.*\)
  • Now we print both the patterns with semicolon in between: \1,\2

Note that the number 10 can replaced by number of characters to be extracted before the delimiter , [0-9] can be replaced by . if these characters can be any type of characters.

Solution 1:

Using cut will be easiest for you in this case.

You first need to get desired fields (2,4) filtered from the line and then do more filtering (only 10 characters from field #2)

$ cut -d',' -f2,4 test.txt | cut -c3-        
8889986665,CRBT123
2398456781,SEGP

Upvotes: 3

bian
bian

Reputation: 1456

sed -r 's/[^,]+,..([^,]+,)([^,]+,)([^,]+),.*/\1\3/' file
8889986665,CRBT123
2398456781,SEGP

Upvotes: 0

anubhava
anubhava

Reputation: 784998

This is job best done using awk:

awk -F, -v n=10 '{print substr($2, length($2)-n+1, n) FS $5}' file

8889986665,CRBT
2398456781,SEGP30

substr command will print last n characters in 2nd column.

Upvotes: 1

Related Questions