comparing 2 strings in shell script
Hi,
My file has entries as follows: root:x:678:79789:home welcome:x:698:79489:temp welcometomyhome:x:698:79489:tmp And I have a sting welcome stored in a variable called variable. I want to check for the string stored in variable through shell scipt. It should only give me welcome from my file. It should not give welcometomyhome (but this also has welcome in it). Can anyone please help me in this regard? Thank you, Dhanabalan. |
Assuming your text file is named `welcome.txt',
create a script named welcome.sh: #!/bin/sh variable="welcome" export string=$variable awk -F: '$1 == ENVIRON["string"]' welcome.txt After making the script executable: $ ./welcome.sh welcome:x:698:79489:temp Ljubomir |
The below code will print the exact line you wanted
------------------------------ #!/bin/bash variable="welcome" grep "^$variable\:" welcome.txt -------------------------------- |
Hi.
Some versions of grep have a "word" search option, "-w": Code:
#!/bin/sh Code:
% ./s1 |
All times are GMT -5. The time now is 07:34 AM. |