Matching string from input to string of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching string from input to string of file
# 1  
Old 10-10-2011
Error Matching string from input to string of file

Hi,
i want to know how to compare string of file with input string

im trying following code:
Code:
file_no=`paste -s -d  "||||\n" a.txt | cut -c 1`
#it will return collection number from file

echo "enter number"
read  " curr_no"

if [ file_no=curr_no]; then
echo " current number already present"
fi

that's it...
but it was not working...please help to resolve my problem
Thanks in advance.....

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 10-10-2011 at 12:52 PM..
# 2  
Old 10-10-2011
Please, display a sample of your input and desired output.
# 3  
Old 10-10-2011
contents of file:
Code:
1
scott
mexico
2
john
new delhi

im trying to match numbers of above file with input using 'read'

Last edited by Scott; 10-10-2011 at 04:16 PM.. Reason: Code tags
# 4  
Old 10-10-2011
Is this what you want:
Code:
#!/usr/bin/ksh
echo "Enter number:"
read mNbr
mRC=$(egrep -c "${mNbr}" < File)
if [[ "${mRC}" != "0" ]]; then
  echo "Number <${mNbr}> already present."
fi

# 5  
Old 10-10-2011
Quote:
Originally Posted by a_smith
contents of file:
1
scott
mexico
2
john
new delhi

im trying to match numbers of above file with input using 'read'
Code:
#!/bin/bash
#file_no=`paste -s -d  "||||\n" a.txt | cut -c 1`
#it will return collection number from file

# maybe you can use like this instead of above cmd
# file_no=`paste -s -d  "||\n" a.txt|cut -d'|' -f1`

## but you must use grep for this achieve to me
file_nos=$(grep '^[0-9]*$' a.txt)
## at the now file_nos = "1 2"
## you can use echo now!!
## echo $file_nos is like seem -> 1 2 (with D_FS)
## echo with double quotes for literal values between quotes so we save our newlines
## echo "$file_nos" is like -> 1\n2

echo "enter number"
read "curr_no"
## for exa you entered multiple nrs --> 1 2
## best method is grep for compare any values
## but we must use split to lines for best comparing..
## so our elements are "1 2"
## if compare its for multiple elements with grep
## then we change spaces to newline for grep search
## we can use tr command
## we try to tr command for clearly line comparing
## and you can use $(commands) exprs instead of (``) backticks
new_curr_no=$(echo "$curr_no"|tr ' ' '\n')

## so above command is equal almost! with below cmds
# new_curr_no=`echo "$curr_no"|tr ' ' '\n'`
## and new_curr_no="1\n2"
## new_curr=
#1
#2

# at the now we can assign an array for complete best comparing
new_curr_no_arr=($new_curr_no)
# new_curr_no_arr[0]=1
# new_curr_no_arr[1]=2

# use for loop \for one-to-one the elements of comparison
for nr in ${new_curr_no_arr[@]}
do

## we can use grep with -w for 'word regexp'
if [[ $(echo $nr|grep -w "$file_nos") ]]

## if the grep cmd return to shell with success status so it is 0
## if the grep exit status is 0 then grep is success so means there is in..
then
## then write your message
echo "$nr number already present"

else
## if not
echo "$nr number is NOT present!!"
## the end of fi
fi
## the end of for
done

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all lines from file matching a string

I wish to search and delete all lines in /app/Jenkins/deploy.txt having this filename string /app/Jenkins/file2.mrt as entry: I'm using : colon as delimiter in sed command as I'm dealing with file paths. Below is the command I was expecting to work. sed -i ":/app/Jenkins/file2.mrt:d"... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

3. Shell Programming and Scripting

How to find previous string based on an input string?

Hi, I did some research but cannot find the right solution so hopefully someone can help me here. I have a long string format like: VAR=111:aaaa,222:bbb,333:ccc it could be VAR=111:aaa,222:bbb,333:ccc,444:ddd, etc what I looking for is eg. if I give ccc, it will return me 333... (2 Replies)
Discussion started by: netbanker
2 Replies

4. Shell Programming and Scripting

Match string from two file input

Hello all, I have file like this: file 1: aa bb cc dd ee file2: 111 111 111 111 111 111 (2 Replies)
Discussion started by: attila
2 Replies

5. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

6. Shell Programming and Scripting

input a string and copy lines from a file with that string on it

i have a file1 with many lines. i have a script that will let me input a string. for example, APPLE. what i need to do is to copy all lines from file1 where i can find APPLE or any string that i specify and paste in on file 2 thanks in advance! (4 Replies)
Discussion started by: engr.jay
4 Replies

7. Shell Programming and Scripting

Get matching string pattern from a file

Hi, file -> temp.txt cat temp.txt /home/pradeep/123/a_asp.html /home/pradeep/123/a_asp1.html /home/pradeep/435/a_asp2.html /home/pradeep/arun/abc/a_dfr.html /home/pradeep/arun/123/a_kir.html /home/pradeep/123/arun/a_dir.html .... .... .. i need to get a_*.html(bolded strings... (4 Replies)
Discussion started by: pradebban
4 Replies

8. Shell Programming and Scripting

Matching a string (zip code) from a list in a separate file

I have a list of postal addresses and I need to pull the records that match a list of zip codes in a separate file. The postal addresses are fixed width. The zip code is located in character position 149-157. Something better than: cat postalfile.txt | grep -f zipcodes.txt would be great. $... (8 Replies)
Discussion started by: sitney
8 Replies

9. Shell Programming and Scripting

Search array elements as file for a matching string

I would like to find a list of files in a directory less than 2 days old and put them into an array variable. And then search for each file in the array for a matching string say "Return-code= 0". If it matches, then display the array element with a message as "OK". Your help will be greatly... (1 Reply)
Discussion started by: mkbaral
1 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question