Extracting text segments with sed
Posted August 30, 2008 at 11:08pm in Linux with tags Linux, sed, Text Manipulation
A couple days ago a friend sent an email to a mailing list I am on wondering how to replace the first 99 characters of a line and extracting characters 100-106 inclusive.
cat file | sed 's/^.\{99\}\(.\{7\}\).*/\1/g'
I will let you look up the regex to know what each part is doing. You would need to do this on a per line basis if you wanted to get 100-106 from each line.
I realize that this is somewhat poor design since I don’t need to cat the file to sed, sed will handle reading from the file. I was just showing that the output needed to be sent to sed for the purpose my friend was looking for. In a real app that needed the functionality you would process the file line by line and pass that line to the sed command to grab the 7 characters.
