Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

how to print the line in case the first field start with Linux1

for example:

  echo Linux1_ver2  12542 kernel-update  |   awk '{if ($1 ~ Linux1 ) print $0;}'

the target is to print the line , while the first field start with Linux1

example of lines:

Linux1-new  36352 Version:true
Linux1-1625543  9847
Linux1:16254 8467563 

remark - space or TAB could be before the first filed

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

One way:

echo "Linux1_ver2  12542 kernel-update"  |  awk '$1 ~ /^ *Linux1/'
share|improve this answer
    
$1 and ^ are redundant. Your proposal doesn't cover tabs. –  Hauke Laging Apr 17 '13 at 10:40
add comment

This should work for this specific case.

awk '/^[[:blank:]]*Linux1/ {print}'
share|improve this answer
    
but if space is before Linux1 the it will no match –  yael Apr 17 '13 at 10:27
    
@yael I made an edit to correct that. –  Hauke Laging Apr 17 '13 at 10:36
add comment

awk ignores leading blanks when assigning fields. The default command is print.

awk '$1 ~ /^Linux1/'

Is what you want.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.