LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-28-2017, 06:38 AM   #1
wedtorque
LQ Newbie
 
Registered: Apr 2017
Posts: 6

Rep: Reputation: Disabled
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
 
Old 04-28-2017, 08:03 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017

Rep: Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196
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
 
Old 04-28-2017, 08:09 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,814
Blog Entries: 4

Rep: Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981
awk could do this, or the full-on programming language perl.
 
Old 06-16-2017, 01:15 AM   #4
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
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.

Last edited by sweepnine; 06-16-2017 at 01:18 AM.
 
Old 06-16-2017, 06:11 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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

Last edited by BW-userx; 06-16-2017 at 06:43 PM.
 
Old 06-16-2017, 07:10 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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#
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to delete everything from a file, once a particular pattern is found, including the pattern ansh007 Linux - General 1 03-07-2017 08:15 AM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
Problem with Slackbuilds - child process / file not found spoovy Slackware 5 11-25-2010 04:33 AM
Shell script to delete file if a pattern is found and restart servers sun81 Linux - Newbie 2 07-22-2010 02:24 PM
Devil-Linux boot process hangs after config file is found. meal3837 Linux - Distributions 0 08-11-2006 01:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration