bash shell script string comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash shell script string comparison
# 1  
Old 04-29-2009
bash shell script string comparison

I want to remove a line that has empty string at second field when I use cut with delimeter , like below
Code:
$cat demo
hello, mum
hello,
 
#!/bin/sh
 
while read line
do
if [ `cut -f 2 -d "," "$line" = "" ]
then
    # remove the current line command goes here
fi
done < "demo"

i got an error message for above command and I have no idea how to remove current line

can I use
Code:
sed 's/$0//'

# 2  
Old 04-29-2009
use something like this

awk -F"," '{ if ($2!=" ") print $0}'
# 3  
Old 04-29-2009
Code:
awk -F, '$2' file > newfile

Regards
# 4  
Old 04-29-2009
I am using if statement and I fixed it to
Code:
if [ `echo $line | cut -f 2 -d","`=="" ]
then
    echo ""
else
    echo $line
fi

but I get an error message below
[: ==: unary operator expected

can any one help me using if statement??
# 5  
Old 04-29-2009

Code:
if [ "`echo $line | cut -f 2 -d","`" = "" ]

Or:

Code:
if [ -z "`echo $line | cut -f 2 -d","`" ]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash not equal string comparison

Why does this cause an infinite loop? #!/bin/bash while ]; do echo "$(ipcs | awk '{print $2}')" done echo exit 0 I have verified I eventually get Semaphore so it should break out of the while loop. $ echo $(ipcs | awk '{print $2}') Shared shmid 262145 294914... (6 Replies)
Discussion started by: cokedude
6 Replies

2. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

3. Shell Programming and Scripting

String comparison using bash

I have a program to create a directory if not present. Here is the program. FYI: Directory name format: YYYY_MM_DD #!/bin/bash date=`date +%Y_%m_%d` presence=$(ls -lrt /TS_File/ | grep "$date" | awk '{print $9}') if then mkdir /TS_File/$date else echo "Unable to Create... (5 Replies)
Discussion started by: nthiruvenkatam
5 Replies

4. Shell Programming and Scripting

SPLIT STRING in bash shell script

i need one help.... if i have a string like aaaaa,bbbbb,ccccc,aaaaa How to to split the string and check howmany times aaaaa will be in that string? Thanks (7 Replies)
Discussion started by: karthinvk
7 Replies

5. Solaris

String Comparison in Shell script

I Have a script which gets the status of oracle database and if the status is READ WRITE ..it should echo "db is up " else "db is down" Here is the code if then echo "db up" else echo "db down" fi done; The script is giving me out put "db down" even thoug the value of... (6 Replies)
Discussion started by: njafri
6 Replies

6. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

7. Shell Programming and Scripting

need help in writing a comparison shell script

I have a folder a1 with the following files sample_1.log sample_2.log sample_3.log sample_4.log sample_5.log sample_6.log In another folder there is a file b with the value 5 My script should take the value 5 ( file b), compare it with the files in folder a1, if file name contains... (1 Reply)
Discussion started by: Nagesh1
1 Replies

8. Shell Programming and Scripting

problem in string comparison in shell programming

Hello, was just wondering how to compare strings in unix? I mean as in C there is a function strcmp() in string.h, is there any function in unix for that? I tried using if and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance :) (9 Replies)
Discussion started by: salman4u
9 Replies

9. AIX

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (1 Reply)
Discussion started by: amarnath
1 Replies

10. Shell Programming and Scripting

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (3 Replies)
Discussion started by: amarnath
3 Replies
Login or Register to Ask a Question