Find number begins with 24

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Find number begins with 24
# 1  
Old 04-27-2017
Find number begins with 24

Hi Team,

i need to list only those number from the input file which begin with 24
Code:
input file is 
2412
2413
2456
2134
2134
2244
2526

Code:
expected output i want is 
2412
2413
2456

# 2  
Old 04-27-2017
Hi, have you tried to use grep ?
# 3  
Old 04-27-2017
It is always worth while using the interactive interpreter first:-
This is a simple longhand starter that requires nothing but a bash terminal.
OSX 10.12.4, default bash terminal.
Code:
Last login: Thu Apr 27 10:53:04 on ttys000
AMIGA:amiga~> echo '2412
> 2413
> 2456
> 2134
> 2134
> 2244
> 2526' > /tmp/24.txt
AMIGA:amiga~> while read -r line; do if [ "${line:0:2}" == "24" ]; then echo "${line}"; fi; done < /tmp/24.txt
2412
2413
2456
AMIGA:amiga~> _

# 4  
Old 04-27-2017
@wisecracker, when using the shell's read command rather than a line processing external utility to process all the characters on the line, it is best to use IFS= to prevent the interpretation of whitespace (otherwise it would also match lines starting with 24). Likewise when printing the result, it is best to use printf to avoid interpretation by the non-standard echo command, that you previously avoided with the -r option.

So:
Code:
while IFS= read -r line
do
  ....
  printf "%s\n" "$line"
done < file



--
Note:
if
Code:
[ "${line:0:2}" == "24" ]

should syntactically be either
Code:
[ "${line:0:2}" = 24 ]

or
Code:
[[ ${line:0:2} == 24 ]]

since ${var:pos:num} is bash/ksh93/zsh, one might also use:
Code:
[[ $line == 24* ]]


Last edited by Scrutinizer; 04-27-2017 at 07:21 AM..
# 5  
Old 04-27-2017
Hi

sorry, but I am not able to understand you suggestion

the number is present on the 18th field in file.

I tried below syntax but it is giving correct output on few nodes and on few nodes it is giving incorrect value

Code:
 
 cat v.txt | cut -d',' -f18 | grep -i ^24 | wc -l

# 6  
Old 04-27-2017
Try:
Code:
awk -F, '$18~/^24/' file

If that is not working, then could you please post a representative sample of your input file and the desired output..
# 7  
Old 04-27-2017
Code:
awk -F ','   '$18 ~ /^24/ { print $18 }' inputfile > outputfile

This prints 18th field where that field number - 18 - starts with a 24.


Your grep command does not match what you actually need, and neglected to mention the field requirement. It helps both you and the people helping you to give enough information to start with. The more you assume the less we can help you.

Code:
wc -l

counts lines it does not print field #18. If you want find how many lines match then pipe the output of the above awk into wc -l
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

Append text to line if begins with pattern1 AND does not end with pattern2

Hello, I'm looking for sed solution to change ... <li>keyword</li> <li>keyword <li>keyword</li> <li>keyword <li>keyword</li> ... to ... <li>keyword</li> <li>keyword</li> <li>keyword</li> <li>keyword</li> <li>keyword</li> ... I.e., if lines beginning with <li> do not end with... (3 Replies)
Discussion started by: pioavi
3 Replies

3. Shell Programming and Scripting

Perl if field begins with.......

Hi, This must be simple but I can't get it to work. I have the follow code to insert the contents of a file into an array and then I want to print the value of a container where all of the records in another container within the array start with 33 (that's not all I want to do but it is all I... (2 Replies)
Discussion started by: Donkey25
2 Replies

4. Shell Programming and Scripting

Printing the column that begins with certain words/numbers

Hi guys, I have got a file which doesn't have the same number of columns in each line. I would like to print the second column and the one that begins with 33= and has some numbers after '33=' Can you please help me asap? Cheers (7 Replies)
Discussion started by: alexandra_ola
7 Replies

5. UNIX for Dummies Questions & Answers

how to tar directories that begins with 'sample_ZZZ'????

Hi, I have a bunch of images (8k) in several directories. I want to tar these directories up and unzip them to development and production with the same path. Example: /about/images/sample_01 /about/images/sample_02 /about/images/sample_03 /about/images/lorem_ipsum... (4 Replies)
Discussion started by: andylee80
4 Replies

6. Shell Programming and Scripting

[awk]: Row begins by random number and field 10 is greater than 10.00%

Hello! I wish to extract the pid where CPU is above 10% last pid: 22621; load averages: 4.71, 5.04, 5.13 15:08:34 221 processes: 212 sleeping, 2 running, 1 stopped, 6 on cpu CPU states: %... (3 Replies)
Discussion started by: Lomic
3 Replies

7. UNIX for Dummies Questions & Answers

remane a file that begins with $$$

I have a file named: $$$.p0001.ps how can I rename this file? I'm using the C shell. -westcoldspring (3 Replies)
Discussion started by: westcoldspring
3 Replies
Login or Register to Ask a Question