Trying to retreive and compare value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Trying to retreive and compare value
# 1  
Old 03-07-2007
Trying to retreive and compare value

Hi All,

I'm have a file test.txt that looks like this

1939399393
03094994949
948383


I have to check whether the first character in the first line is 1 or not. I have tried the following option but it seems to fail

head -1 test.txt | grep ^1

but it seems to display the entire first line without validating

Any suggestions.

Thanks
# 2  
Old 03-07-2007
Code:
sed -e "s/^\(.\).*/\1/g;q" | grep "1"

# 3  
Old 03-07-2007
Code:
if [[ -n $( sed -n "/^1/s/.*/1/p;q" file ) ]]
then
   echo "First line contain 1 as first char"
fi

# 4  
Old 03-09-2007
CPU & Memory

Quote:
Originally Posted by omahablues
Hi All,

I'm have a file test.txt that looks like this

1939399393
03094994949
948383


I have to check whether the first character in the first line is 1 or not. I have tried the following option but it seems to fail

head -1 test.txt | grep ^1

but it seems to display the entire first line without validating
You don't need any external commands to do that:

Code:
read line < text.txt
case $line in
   1*) echo "First character is 1" ;;
   *) echo "First character is not 1" ;;
esac

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How To Retreive Files With a Special Condition?

Everyday I have to get a list of files in a directory with a special condition and feed this list to a for loop to be processed. Since I do not use Unix all the time, it is tricky for me to get that list of files. So, the question is whether there are commands that will give me the file names... (12 Replies)
Discussion started by: april
12 Replies

2. Shell Programming and Scripting

Search and retreive after matched words

I have following source file format {"driver":{"first_name":"xxxx","last_name":"yyyy"},"confirmation_id":"US285204420","vendor":{"id":"1234","name":"BUDGET"}} I need to extract the data from above and provide in below desired output is xxxx,yyyy,US285204420,1234,BUDGET Can you please... (11 Replies)
Discussion started by: nqp200
11 Replies

3. Shell Programming and Scripting

Retreive data with arrangement

Hi all I have following part of a big file TTDS00002 Synonyms M1 receptor TTDS00002 Disease Alzheimer's disease TTDS00002 Disease Bronchospasm (histamine induced) TTDS00002 Disease Cognitive deficits TTDS00002 Disease Schizophrenia TTDS00002 Function The muscarinic acetylcholine... (2 Replies)
Discussion started by: kareena
2 Replies

4. Shell Programming and Scripting

how to retreive certain section of the line

Hi I am using "grep" command to get certain pattern out of the file: PNUM=34 $ grep -w "#${PNUM}" myfile #34 * 2297 * 410 * 964 * * 4352 $ Is there a way to retrieve the section of the above output without #34 so the output would look like this:... (3 Replies)
Discussion started by: aoussenko
3 Replies

5. UNIX for Dummies Questions & Answers

Cannot retreive correct $PATH using PLINK

Hi, I'm using plink to execute shell script on UNIX machines. It works pretty well excepted with some machines where I don't have the same $PATH than with putty. I'm using the command PLINK.EXE -ssh machinename -l user -pw password echo $PATHAnd for putty nothing special set, I use ssh as... (2 Replies)
Discussion started by: Peuj
2 Replies

6. UNIX for Advanced & Expert Users

fast way to retreive a list of lines

hi there, i have to read lines from the file, where the line is just above the pattern am looking for typically this looks like this <time-stamp>|----- <pattern am searching >...... <time-stamp>|..... <some garbage > .... the log file is big wc -l ~/log/ompe.log.20081203... (8 Replies)
Discussion started by: kiranreddy1215
8 Replies

7. Shell Programming and Scripting

how to retreive a block of data from the file

Hi I am running a 'grep' command to retrieve a line from the file. The problem is that I also need 21 lines which go right after the line I just 'grep'(ed) Is there a way to retrieve this block of data? Thanks -A (4 Replies)
Discussion started by: aoussenko
4 Replies

8. Shell Programming and Scripting

how to retreive a block of data from the file

Hi I have a several thousands lines text file. Is there any command(s), which would allow me to retreive a block of data between two specified lines of this file? Thanks a lot -A (2 Replies)
Discussion started by: aoussenko
2 Replies

9. Shell Programming and Scripting

Retreive string between two chars

I want to write a shell script in order to retreive some data from a log file that i have written into. The string that i want to get is the number 2849 (that is located between | | ). To explain further, this is the result i get after running "grep LOGIN filename.log" but i need to get the... (25 Replies)
Discussion started by: danland
25 Replies

10. UNIX for Advanced & Expert Users

Retreive process output

Hi, I had a process that was producing a standard output (no log of it eing produced), unfortunalty the xterm it was running in died and I lost the output. I have logged back in and can see that the process didn't die. How can I bring this process to the foreground so that I can see the output?... (2 Replies)
Discussion started by: nhatch
2 Replies
Login or Register to Ask a Question