technopathe technopathe - 3 months ago 11
Bash Question

extract value by position and maximum length

I have to extract the pattern at ( Position 13 , Maxlength= 10 ) from files :

File 1 :

fjlksflsf content1 blabla
kjodeddek content1 blabla
fdfkjlsdd content1 blabla
fsdffsdfs content1 blabla
.
.
.
dzedojioj content1 blabla


I would like to use a script to extract the value "content1"

File 2

fjlsdfsf content22 blabla
gfgttsdd content22 blabla
gzdfldfd content22 blabla
azefsgtg content22 blabla
.
.
.
fsffsdfj content22 blabla


Same thing here the script should loop the files and extract the right value at the position 13 till the position 23 then print it on the screen, for exemple the extracted value from the second file is "content22"

Answer

In AWK. If you really want from position 13, length 10:

$ awk '{print substr($0,13,10)}' file1
 content1 
 content1 
 ...

You could also just print the second field:

$ awk '{print $2}' file1
content1 
content1 
...
Comments