LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to process a file after a pattern is found (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-process-a-file-after-a-pattern-is-found-4175604855/)

wedtorque 04-28-2017 06:38 AM

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

grail 04-28-2017 08:03 AM

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

sundialsvcs 04-28-2017 08:09 AM

awk could do this, or the full-on programming language perl.

sweepnine 06-16-2017 01:15 AM

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.

BW-userx 06-16-2017 06:11 PM

Code:

#!/bin/bash


while ISF= read -r wd
do
if [[ "$wd" =~ "#" ]] ; then
    vowel=${wd:0:1}
  if [[ "$vowel" = [a,e,i,o,u] ]] ; then
  {
    addDollar="\$$wd"
    echo "addDollar: $addDollar"
    #replace what word with added $ here.
    sed  -i "s/$wd/$addDollar/g" "$1"
  }
  fi
fi
done < "$1"

results off command line.
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
addDollar: $is awesome#
addDollar: $america#

you're going to have to figure out how to check strings for a space - if [space] then reject string, move to next string/line

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
 when putting two $$ together as the $ is a reserved
 variable/function in bash. ( i do not know the proper
 techy name for that $ but that is the jist of it)

var="\$$var1"

MOD:
I just checked it is two months old Oh well

BW-userx 06-16-2017 07:10 PM

Might as well finish it to a point.
Code:

#!/bin/bash
while ISF= read -r wd
do
if [[ "$wd" =~ "#" ]] ; then
    wd=${wd#* }
    vowel=${wd:0:1}
  if [[ "$vowel" = [a,e,i,o,u] ]] ; then
  {
    addDollar="\$$wd"
    echo "addDollar: $addDollar"
    #replace what word with $ here.
    #because this does not work -- :)
    sed  -i "s/$wd/$addDollar/g" "$1"
  }
  fi
fi
done < "$1"



results of file
Code:

------this is a comment1---
rose#
marigold#
------this is a comment2---
dog#
cat#
------pattern-------
------this is a comment3---
brazil
is $awesome#
$america#

---egdg-dg-dg---
$elefent#



All times are GMT -5. The time now is 12:43 AM.