how to process a file after a pattern is found
i have a file that contains some text with comments i have to change the text ending with a special character to something else without changing any comment
example ------this is a comment1--- rose# marigold# ------this is a comment2--- dog# cat# ------pattern------- ------this is a comment3--- brazil is awesome# america# what i needed 1.look for the "-----pattern----" start reading from there 2.skip the comments 3.store the string(important) in a variable till "#" is found.ADD "$" if the string start with vowel. 4.keep doing till the end of file My final file should look like this ------this is a comment1--- rose# marigold# ------this is a comment2--- dog# cat# ------pattern------- ------this is a comment3--- brazil is awesome# $america# this is what i have tried awk '/----Pattern----/'{ while IFS="#" read -r LINE || [[ -n "$LINE" ]]; do if echo "$LINE" |grep -q '^--'; then continue else if echo "$LINE" |grep -q '^a'; then LINE="$""$LINE" done } file.txt *this is not working .:( i have less expierence with unix |
Firstly, please use [code][/code] tags around your code and data to keep formatting and make clearer.
Second, pick a language ... that is not meant to be nasty, but you have combined both awk and shell (maybe bash, you do not advise) and in a way that will not work. As for what you want to achieve, perform each of the steps you have outlined one at a time and build your script. Either awk OR bash can do what you need. Here is a little flesh out based on your criteria: 1.look for the "-----pattern----" start reading from there --- Once you find your pattern, set a variable to true to tell the rest of your code when to start the next process (sounds like an 'if') 2.skip the comments --- After you find the above you will need to look at the lines to be processed to see if they are a comment (for this you need to know what makes a comment, ie. the string 'comment' or a symbol, like in bash it is the #) (sounds like another 'if' 3.store the string(important) in a variable till "#" is found.ADD "$" if the string start with vowel. --- this one will really depend on the language you use, but the options would be, use a regular expression to check first character or some kind of sub-string method to remove the first character for testing ... and of course save the word itself 4.keep doing till the end of file --- Again this is language specific, but the main process would be a while loop in bash or with awk it does exactly this, ie. read a file until the end See how you get on :) Here are some links that may assist: http://tldp.org/LDP/abs/html/ http://www.gnu.org/software/gawk/man...ode/index.html |
awk could do this, or the full-on programming language perl.
|
Sounds doable. If you have trouble with that you better do as suggested by sundialsvcs and go for a friendly language such as python. With bash you wont end tinkering on little, strange issues for some while. Once your are able to deal with them in a reasonable amount of time, you realize that bash is a bad choice for this kind of task.
|
Code:
#!/bin/bash still needs a little work... as you can see and the adding the proper results to the file itself as well. Code:
userx%slackwhere ⚡ testing ⚡> ./find-replace-vowles vowle-replace-test-file to understand sub strings and the striping of the words that matched read this http://tldp.org/LDP/abs/html/string-manipulation.html and know this Code:
the backslash escapes the char $ so no error is given I just checked it is two months old Oh well |
Might as well finish it to a point.
Code:
#!/bin/bash results of file Code:
------this is a comment1--- |
All times are GMT -5. The time now is 12:43 AM. |