-2

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"

4
  • Where do there column and length arguments come from? Would it be easier to just specify column #2?
    – shawnt00
    Aug 24, 2016 at 0:16
  • position 13 and maxlength 10 are extremely vague terms. character position in a line, line position in a file, string position in a line, length of a word, length of a line..... edit your question to clearly state your requirements and get rid of all the ...s change all the words "content" to ACTUAL representative content and provide sample input/output that we could test a potential solution against.
    – Ed Morton
    Aug 24, 2016 at 0:21
  • 1
    What have you tried? I would recommend adding that to your question, otherwise your "question" seems more like it's a request for "free script coding". Also, you don't need a "script" to do this anyway — it's literally one command perhaps if you'd try searching for the term cut you'll likely find your answer.
    – l'L'l
    Aug 24, 2016 at 0:21
  • awk '{ print substr($0, 13, 10) }' would be a starting point. If you want to trim trailing whitespace, that should not be too hard to figure out.
    – tripleee
    Aug 24, 2016 at 2:35

2 Answers 2

0

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 
...
0

Try this one:

sed -e 's/.\{12\}\(.\{10\}\).*/\1/'

It will:

  • .\{12\} - look for first 12 characters

  • \(.\{10\}\) - will match next 10 characters

  • .* - rest of the line
  • \1 - will print only matched 10 characters

Of course you would have to change the number of characters you want to ignore and match.

Your Answer

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

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