grep lines separated with semicolon


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep lines separated with semicolon
# 1  
Old 07-30-2012
grep lines separated with semicolon

Hello,

I would like to kindly ask you for help. I have a file with some lines in one row separated by semicolon. I need to find out, if the line I have in different variable is included in this file. e.g

I have a file foo.txt with lines
Code:
A=hello there;hello world;hello there world

In the my shell script I grep the lines separated with semicolon like:
Code:
CONST_A=`cat foo.txt | grep "A=" | cut -d "=" -f1`

CONST_A now contains:
Code:
hello there;hello world;hello there world

Now I have different variable, which contains string "hello there" and I want to find if the constant contains exactly this string. What would be the best way to do that? Is it grep or awk? And how? Thank you very much for your answer.
# 2  
Old 07-30-2012
how about this one

Code:
echo "A=hello there;hello world;hello there world" | sed '/hello there/s/^[a-zA-Z]*=//g'

# 3  
Old 07-30-2012
Just use the shell test function:

Code:
CONST_A=$(sed -n '/A=/s/^.*=//p' foo.txt)
VAL_A="hellow there"

if [ "$CONST_A" = "$VAL_A" ]
then
   echo "Exact match"
fi
 
if [[ "$CONST_A" = *"$VAL_A"* ]]
then
    echo "$VAL_A is in the string"
fi


Last edited by Chubler_XL; 07-30-2012 at 07:27 PM..
# 4  
Old 07-30-2012
Take for example
Code:
KEY=hello world;foobar;another

You search for "foo". You would want to find "foobar" and return success? Probably not. So you want to find [icode](^|Smiliefoo(;|$)[icode] only.
Depending on shell... you could do the entire of it in bash if it is small configuration or some such:

Code:
$ IFS='=' read -r key val <<< "$str"
$ echo "[[$key]] [[$val]]"
[[KEY]] [[hello world;foobar;another]]
$ IFS=';' read -ra vals <<< "$val"
$ declare -p vals
declare -a vals='([0]="hello world" [1]="foobar" [2]="another")'
$ in_array() { find=$1; shift; for v; do [[ $v = $find ]] && return 0; done; return 1; }
$ in_array foo "${vals[@]}"; echo $?
1
$ in_array foobar "${vals[@]}"; echo $?
0

# 5  
Old 07-30-2012
You could also dummy ";" on front and back
then search for ";value;" eg:

Code:
$ VALS="hello world;foobar;another"
$ [[ ";$VALS;" = *";hello world;"* ]] && echo yes
yes

# 6  
Old 07-31-2012
Try with AWK:

Code:
awk -F";" '
/^A=/ {
  for ( i=1; i<=NF; ++i ) {
    if (index($i, "hello world")>0 ) {
      print $i
    }
  }
}' foo.txt

# 7  
Old 08-01-2012
Thank you very much for your answers. On the end I used dummy ; and it works good
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace semicolon within double quotes in a file with semicolon delimiter

Hello Team, Could you please help me with the below question? I have a file with the following properties 1) File Delimiter is ; 2) Text columns are within double quotes 3) Numeric columns will not have double quotes 4) File has total 6 columns Please see a sample record from file ... (3 Replies)
Discussion started by: sam99
3 Replies

2. UNIX for Dummies Questions & Answers

Delete a semicolon and numbers after a semicolon

I have this: ((9:0.010,(11:0.089,13:0.004)) and I would like this: ((A9,(A11,A13)) How do I delete the semi colon and the number (i.e. 0.010) after the semi colon? Also, how can I add the letter before the number that is NOT removed? Thank you in advance! ---------- Post updated... (4 Replies)
Discussion started by: MDeBiasse
4 Replies

3. Shell Programming and Scripting

grep on string separated by spaces

hi I am on AIX 5 and i have a script that runs the following command to list processes running. I then want to kill the returned processes. The PID are on field 2 separated by spaces. $ ps -ef|grep "rams.e $PORT" lesqa 1826998 2646248 0 11:20:35 pts/2 0:00 grep rams.e t24cm 2789380 ... (3 Replies)
Discussion started by: dustytina
3 Replies

4. Shell Programming and Scripting

AWK:Split fields separated by semicolon

Hi all, I have a .vcf file which contains 8 coulmns and the data under each column as shown below, CHROM POS ID REF ALT QUAL FILTER INFO 1 3000012 . A G 126 ... (6 Replies)
Discussion started by: mehar
6 Replies

5. Shell Programming and Scripting

Need to concatenate spuriously separated lines

Given the pattern below: 3113296571|NULL|NULL|NULL||N| 1| 0| 926667| 1001036| 0| 3076120438|NULL|NULL|NULL|NULL|DUE FOR NEW CONSENT!|N|NULL| 10198318|2011-07-25-12.34.02.786000|NULL|NULL|NULL| 0 3113336478|NULL|NULL|NULL||N| 1| ... (16 Replies)
Discussion started by: lemele
16 Replies

6. Shell Programming and Scripting

Compare files with fields separated with semicolon

Dear experts I have files like ABD : 5869 events, relative ratio : 1.173800E-01 , sum of ratios : 1.173800E-01 VBD : 12147 events, relative ratio : 2.429400E-01 , sum of ratios : 3.603200E-01 SDF : 17000 events, relative ratio : 3.400000E-01 , sum of ratios : 7.003200E-01 OIP: 14984... (9 Replies)
Discussion started by: Alkass
9 Replies

7. Shell Programming and Scripting

Grep and print next 10 Lines separated by ,

Hi All, I need to grep through a file for a string and print the next ten lines to a file separating the lines with a , and save it as a csv file to open it as a XL file. The 10 lines should be on a sigle row in xl. Any suggesstions please. Note; I dont have a GNU Grep to use -A flag. ... (6 Replies)
Discussion started by: Nani369
6 Replies

8. Shell Programming and Scripting

Delete lines after semicolon

char str; char str ; char *ptr; char * ptr; int CASE; int CASE; double temp; double temp; Output should be: char str; char *ptr; int CASE; double temp; How can i do this with awk,sed,perl? (5 Replies)
Discussion started by: cola
5 Replies

9. Shell Programming and Scripting

Extract semicolon separated delimiter

The log reads as follows. fname1;lname1;eid1;addr;pincode1; fname2;lname2;eid2;addr2;pincode2; fname3;lname3;eid3;addr3;pincode3; fname4;lname4;eid;addr4;pincode4; how do i extract only fname and save it in an array similarly for lname and so on i tried reading a file and cutting each... (5 Replies)
Discussion started by: vkca
5 Replies

10. Shell Programming and Scripting

two lines into one colon separated line...

Does anyone know how to get these two output lines into one colon ':' separated line with some unix command? Maybe nawk. I've tried to read the nawk and awk man pages but I don't get it right. Are these commands the one to use? Output from find command: # /sw/tools/matlab/7.0.1/man... (2 Replies)
Discussion started by: tonlu
2 Replies
Login or Register to Ask a Question