Home > database >  Read the cat output with specific range of the letters
Read the cat output with specific range of the letters

Time:08-18

I digged the stackoverlow and Superuse but I couldn't find answer for my need.

I need to read the output with range of line.

cat try.txt

00aa00nnn0123 Just trying to know

I just want to read the line of range 1 to 13 f.e just this line

00aa00nnn0123

The file just has one line.

Maybe answer will be so simple but I really struggle with this.

Thanks

CodePudding user response:

I just want to read the line of range 1 to 13

Josué Carvajal's answer will read the first word for the file. If you really want to extract the text by the character offset 1 to 13, you can use the cut command:

$ cut -c 1-13 try.txt 
00aa00nnn0123

The cut command can also extract based on a delimiter, for example:

cut -d' ' -f 1 try.txt

CodePudding user response:

If your file follows that pattern for example:

00aa00nnn0123 Just trying to know0
00aa00nnn0124 Just trying to know1
00aa00nnn0125 Just trying to know2

You can use awk to achieve this, for example

❯ cat try.txt| awk '{print $1}'                                                                                                
00aa00nnn0123
00aa00nnn0124
00aa00nnn0125

Where the $1 is the column you want to retrieve, in this case, the first one.

I hope this is what you are looking for.

CodePudding user response:

Using Bash (substring) parameter expansion syntax ${str:position:length}

Read contents of try.txt into variable

var=$(<try.txt)

Now extract first 13 characters from variable

echo "${var:0:13}"
00aa00nnn0123
  • Related