I have a file for eg with three coloumns

12345678910 14567855858855 12345678510750078

I want to consider only first 10 digits from the third coloumn with sed or awk.

expected ouput is:


Please help


You could try:

awk '{ print $3; }' subject.txt | sed -n 's/\([0-9]\{10\}\).*/\1/p'
  • or awk '{print substr($3,1,10)}' subject.txt – dave_thompson_085 Oct 2 '16 at 13:01

This sed command will give you the first 10 digits of the last column.
Your question is a bit confused in terms of first or last 10 or 14 digits :-)
But, you can adjust likewise on this example.

$ echo "12345678910 14567855858855 12345678510750078" \ 
| sed -n 's/.*\s\([0-9]\{10\}\)[0-9]*$/\1/ p'


Interpreting the command (so you can modify as required).

 sed -n 's/.*\s\([0-9]\{10\}\)[0-9]*$/\1/ p'
     |   | | | | |          | |       |   ^ print what remains on the matched line
     |   | | | | |          | |       ^^ replace the line with the part of interest
     |   | | | | |          | ^^^^^^^ match for the last column
     |   | | | | |          ^^ mark the end of part we want to print
     |   | | | | ^^^^^^^^^^^ this will match 10 digits at the start of the last column
     |   | | | ^^ start marking the part we want to print
     |   | | ^ start matching the digits after a white-space char
     |   | ^^ pattern begins matching everything up to the part of interest
     |   ^ process only lines that match the given pattern
     ^^ do not print the original input string

You can fine tune this for your data.
As it stands, because of the [0-9]*$ part in this rule your data is expected to have no whitespace or non-numeric characters after or inside the last column.

Update on your comment.
While this example uses an echo of your single line to demonstrate your test-case,
You can fire the command on your entire file as follows,

cat input-file.txt | <sed-command-above> > output-file.txt


<sed-command-above> input-file.txt > output-file.txt

The first form shows how an echo will work for the whole multi-line file too.
You could also do a short test with a head input-file.txt piped to the sed command to see how it works on the first 10 lines of you input file.

  • thanks for your answer.. but my file has some 100 lines. the one line i have given as a sample.. so please give me the sed command to get the first 10 digits of last coloumn not with echo.. – ltps Oct 2 '16 at 7:43
  • thanks for your answer but when i run this command sed -n 's/.*([0-9]\{10\})[0-9]*$/\1/ p' inputfile.txt i am not getting any output – ltps Oct 2 '16 at 7:54
  • when i run with this sed command sed -r 's/.*(.{10})$/\1/' 1.csv i am getting the output but it is considering the last 10 digits from the third coloumn i am gettng output as 510750078 for my input file 12345678910 14567855858855 12345678510750078 – ltps Oct 2 '16 at 7:57
  • Right, my bad, a space in the rule got lost while editing... I've changed that to a \s which is more appropriate and easier to not-lose. – nik Oct 2 '16 at 8:05

Perl to the rescue:

perl -lne 'print /(\d{10})\d*$/' < filename
  • -n reads the input line by line
  • -l adds newlines to output
  • $ matches the end of line, the first 10 digits preceding any other digits before the end are captured, and /.../ in the list context imposed by print returns that
  • thanks for that but it is considering the last digits from reverse order.. i am supposed to get 1234567851 but the output gives 510750078 – ltps Oct 2 '16 at 6:44
  • 1
    @ltps: Oh, so you want the first ten digits of the last column! You should always add the expected output to a question to make it clearer. Answer updated. – choroba Oct 2 '16 at 6:55
  • yes i want the first ten digits of the last column. sorry for that.. will be clear from now on – ltps Oct 2 '16 at 7:38

if you wand sed only solution, try:

cat /tmp/textfile |  sed -n -e '$!d;s/.*\s\([0-9]\{10\}\)[0-9]*$/\1/ p'

the substitute worked only on the last line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.