and I’m used to vi not vim. For my work I am currently trying to make a script using ksh and vi. What I want to do is be able to open and search a file for key terms and extract information. For example I would like to run a ksh script that searches a file for the key term ‘subject’ and then extract the information ‘line one’. The reason I want to use vi is because I then want to write to a file the values I have extracted into one line delimited by commas.
Subject: line one
Content: line two
Thank you for any help and references.
Edit: Yes using sed, awk, and grep is a more effective way. But I have a problem assigning my result to a variable in ksh, currently I have :
testing=grep Primary /u/mtjandr/temp.txt | sed -e 's/[:a-zA-Z]* //g'
grep Primary /u/mtjandr/temp.txt | sed -e 's/[:a-zA-Z]* //g'
By itself it ouputs the right result but when I try assigning it to a variable I get erorr
copy_group.sh[18]: Primary: not found.
Update: I found my solution:
testing=`grep Primary /u/mtjandr/temp.txt | sed -e 's/[:a-zA-Z]* //g'`
Given that your file probably looks regular, you would want to look into
awk,grepandsedas Shawn mentioned.Grepgives you the lines you are interested,awkextracts the fields andsededits the whole thing.So in your example, you would want to find the lines containing
Subject:and extract
line one:and finally remove the extra
,inserted in the end of the fileThese three commands need to be piped together:
Of course, the
awkpart needs to be customized to your exact file structure.Finally, to write the whole thing in another file (instead of output), you can redirect the output of the command above to a file: