LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-09-2014, 12:21 AM   #1
sepide
LQ Newbie
 
Registered: Feb 2014
Posts: 3

Rep: Reputation: Disabled
Unhappy keep groups of lines with specific keywords


Dear all
I have a text file with plenty of lines in this format (the lines between every two # defined as a group):
# some str for test
hdfv 12 9 b
cgj 5 11 t
# another string to examine
kinj 58 96 f
dfg 7 26 u
fds 9 76 j
---
key.txt:
string to
---
output:
# another string to examine
kinj 58 96 f
dfg 7 26 u
fds 9 76 j

I should search some keywords(string,to) from lines which starts with # and if the keywords does not exist in key.txt (a file with two columns) then I should remove that line and the following lines(of that group).I've written this code without result!

Quote:
cat input.txt | while IFS=$'#' read -r -a myarray
do
###a is lines starting with "#",b=following lines of that group
a=${myarray[1]}
b=${myarray[0]}
unset IFS
read -r a x y z <<< "$a"
key=$(echo "$x $y")
if grep "$key" key.txt > /dev/null
then
echo $key exists
else
grep -v -e "$a" -e "$b" input.txt > $$ && mv $$ input.txt
fi
done
can some one help me?(keywords written together in input file)
 
Old 02-09-2014, 01:58 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Does this have to be done in bash?

I would first suggest using set -xv to see if all your variables are being populated the way you think they are.
Code:
read -r a x y z <<< "$a"
Assuming the correct data is stored in variable 'a', x will be first word, y will be second and z will be the rest of the line.
Based on your desired output that would mean from the input file:
Code:
x = another
y = string
z = to examine
So not sure I see how you would ever get the correct output??
 
Old 02-09-2014, 02:26 AM   #3
sepide
LQ Newbie
 
Registered: Feb 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
I did not understand how to set -xv?!
read -r t x y z <<< "$a" ----> t=another
x=string
y=to
z=examine

a=${myarray[1]} ---> some str for test
another string to examine

the code can be written in any language.But I've worked only in bash.
 
Old 02-09-2014, 02:47 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Quote:
I did not understand how to set -xv?!
Type those exact words and letters as the second line of your script:
Code:
#!/bin/bash
set -xv
Quote:
read -r t x y z <<< "$a" ----> t=another
x=string
y=to
z=examine

a=${myarray[1]} ---> some str for test
another string to examine
So you can confirm that the string you are searching for will always be the second and third words? (if they exist at all)

eg. What if the line is:
Code:
# string to examine
 
Old 02-09-2014, 02:55 AM   #5
sepide
LQ Newbie
 
Registered: Feb 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Type those exact words and letters as the second line of your script:
Code:
#!/bin/bash
set -xv
So you can confirm that the string you are searching for will always be the second and third words? (if they exist at all)

eg. What if the line is:
Code:
# string to examine
Input file contains the format as the example I wrote. I'm sure that the first lines of each group have the same length and same number of words. also keywords are always 2nd and 3rd words.
 
Old 02-09-2014, 10:43 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,484

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
With this InFile ...
Code:
# some str for test
hdfv 12 9 b
cgj 5 11 t
# another string to examine
kinj 58 96 f
dfg 7 26 u
fds 9 76 j
... this code ...
Code:
key="string to"
paste -sd~ <$InFile           \
|sed 's/#/\n#/g'              \
|awk -v "key=$key"            \
  '{if (key==$3" "$4) print}' \
|tr "~" "\n"                  \
>$OutFile
... produced this OutFile ...
Code:
# another string to examine
kinj 58 96 f
dfg 7 26 u
fds 9 76 j
Daniel B. Martin

Last edited by danielbmartin; 02-10-2014 at 11:05 AM. Reason: Corrected code
 
  


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
Vim: writing nonconsecutive groups of lines to another file. stf92 Linux - Newbie 2 08-21-2011 10:07 PM
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 06:47 AM
How to Substitute Specific Keywords in a particular line joyds219 Linux - Newbie 4 04-04-2008 04:33 PM
filter out lines containing specific keywords from output kpachopoulos Linux - General 2 03-12-2007 09:29 PM
GCC 4.0 specific keywords abk4523 Programming 1 09-22-2005 01:03 PM


All times are GMT -5. The time now is 12:24 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration