Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

Using sed to copy a string of x-number of characters from a specific line

I am trying to use sed in order to copy partial strings from a log file.

When I wanted to copy a whole line, I used:

for file in ls Dir/logs/*NN*

do

   sed -n 3,3p $file >> log_NN.csv

done

Is there a similar command that will take only part of a string, in a certain location of that line?

For example I have in line 22 of the log the following string, and I want to copy the first number to another file, then the 2nd number to a file, etc...:

step - loss: 0.6844 - acc: 0.6294 - val_loss: 0.5905

Thanks in advance for the help :)

question from:https://stackoverflow.com/questions/65830599/using-sed-to-copy-a-string-of-x-number-of-characters-from-a-specific-line

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
awk 'FNR==22 { print $4 >> file1;print $7 >> file2; print $10 >> file3 }' Dir/logs/*NN*

Awk would be a better candidate for this. Process all the files at once and then when the File number record (FNR - the line in each file) is 22, append the 4th space delimited field to file1, the seventh to file2 and then 10th to file3.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...