Sponsored Content
Top Forums Shell Programming and Scripting Matching string from input to string of file Post 302563257 by ygemici on Monday 10th of October 2011 02:40:03 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
set_color(1)							       fish							      set_color(1)

NAME
set_color - set_color - set the terminal color set_color - set the terminal color Synopsis set_color [-v --version] [-h --help] [-b --background COLOR] [COLOR] Description Change the foreground and/or background color of the terminal. COLOR is one of black, red, green, brown, yellow, blue, magenta, purple, cyan, white and normal. o -b, --background Set the background color o -c, --print-colors Prints a list of all valid color names o -h, --help Display help message and exit o -o, --bold Set bold or extra bright mode o -u, --underline Set underlined mode o -v, --version Display version and exit Calling set_color normal will set the terminal color to whatever is the default color of the terminal. Some terminals use the --bold escape sequence to switch to a brighter color set. On such terminals, set_color white will result in a grey font color, while set_color --bold white will result in a white font color. Not all terminal emulators support all these features. This is not a bug in set_color but a missing feature in the terminal emulator. set_color uses the terminfo database to look up how to change terminal colors on whatever terminal is in use. Some systems have old and incomplete terminfo databases, and may lack color information for terminals that support it. Download and install the latest version of ncurses and recompile fish against it in order to fix this issue. Version 1.23.1 Sun Jan 8 2012 set_color(1)
All times are GMT -4. The time now is 01:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy