search from specified charactor space

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers search from specified charactor space
# 1  
Old 01-30-2008
search from specified charactor space

Hello, I'm quite a noobie in programming in UNIX
and I was wondering if it is possible to use 'grep' or similar method
to find PATTERNS from designated location (of charactor)
for example

|param 1 ||param2 |
andrew kim josh
daniel kim michelle
michelle andrew kim

I hope to be able to search up
kim and display only the first 2 lines not the 3rd

i know that cut has -c1-10 so you can specify charactor to cut
but can the grep or other things be done the same? and display the whole line?

thank you very much
# 2  
Old 01-30-2008

So to clarify, you are wanting to only match lines that have (for example) 'kim' in the first or second field, but display the entire line where it's matched, right?

If so, you'll need a little bit of logic around the grep:
while read line ; do if echo $line | cut -d ' ' -f 1,2 | grep kim > /dev/null ; then echo $line ; fi ; done

# 3  
Old 01-30-2008
Hello Smiling Dragon
Thanks for the post

sorry but what is /dev/null?

and REALLY sorry but i have a small problem
when i do read by line
my ' '(space x 10) is same as ' ' (space x1)
is there a way to keep the spaces as it is while
reading line by line?
thanks (this is important because i cannot use delim as some are like JenniferKim 2 params are stuck together)

Last edited by akmix; 01-30-2008 at 10:07 PM..
# 4  
Old 01-30-2008
I think I follow what you mean...
If you need something to appear in the forums as you've typed it, surround it with CODE tags (the "#" symbol in the editor).

/dev/null is just a garbage can, it means that I want the output from grep to go nowhere as we don't need it. You can get a similar effect by using grep with the -q option but that only wroks on certain versions of grep.

If your fields are exactly 8 chars wide, we can change the cut call in the code like so:
cut -d ' ' -f 1,2

cut -c 1-16

(ie, instead of cutting fields 1 and 2 seperated by spaces, we cut chars 1-8 and 9-16)
# 5  
Old 01-30-2008
oh, thank you
well my code is exactly certain character wide but
when i use read line by line method
it seems to shrink it down
so if i type

cat emplist
Andrew Kim..........Daniel Kim
Henry Kim..........Danny Kim
Daniel Taegyun tk KimAndrew Kim

but read line then echo'ing it gives me
Andrew Kim.Daniel Kim
Henry Kim.Danny Kim
Daniel Taegyun tk KimAndrew Kim

is there -(something) statement that preserves the # #?
sorry my question is starting to go really long..
and I've replaced (space) with . thanks

ps. i'v tried # #? and # # is same as # #?

# 6  
Old 01-30-2008
If we're talking about formatting problems, you must use CODE tags or we'll never understand what you are after.
However, If you want echo to not contract spaces, add quotes around the params passed to it:
then echo $line

then echo "$line"

# 7  
Old 01-31-2008
thank you for your fast reply
I think i've got it working
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command in Linux in a script where the search string has a space

I have a file xyz with the following content PPPL 0123 PPPL 0006 POFT 0923 POFT 1111 WENT 2323 SEND 2345 I also have another file named MasterFile where it contains the above mentioned data million times with different digits at the end for example some times it contains SEND 9999 or WENT... (4 Replies)
Discussion started by: knijjar
4 Replies

2. Shell Programming and Scripting

Insert one charactor multiple times in VI

Hi Gurus, I forgot the command which can insert one character multiple times. for example: I need 50 "#" #############... I used the command before, it is very convenient. anybody can help this. thanks in advance. (1 Reply)
Discussion started by: Torhong
1 Replies

3. Shell Programming and Scripting

Search and repllace of strings with space between words

Dear all, I have gone through all the search and replace requests but none of them meet my particular need. I have a huge file in which all Unicode characters are stored as Names. A sample is given below. I want to replace strings in that file with a mapper from another file termed as master.dic. ... (4 Replies)
Discussion started by: gimley
4 Replies

4. Shell Programming and Scripting

How to check charactor in certain position?

Hi Gurus, I need to check the charactor in certain position. in my file abcdef1234gh ac1234eeeegt acdead1235gh I want to check what is check from position 7 to 10's charactor, if it is 1234, then output the whole line. for above file, I want to get below output abcdef1234gh... (2 Replies)
Discussion started by: ken6503
2 Replies

5. Shell Programming and Scripting

awk search for space character

How do I use awk to search for a string that contains a space bar? I have tried this awk '/send email/ {print $0}' input awk '/send\ email/ {print $0}' input (1 Reply)
Discussion started by: locoroco
1 Replies

6. AIX

grep not working when search string has a space in it

Hi, I am trying to grep a string which has two words separated by space. I used a script to grep the string by reading the string in to a variable command i used in the script is echo "enter your string" read str grep $str <file> it is working fine when the entered string is a single... (3 Replies)
Discussion started by: sekhar gajjala
3 Replies

7. Shell Programming and Scripting

grep : search a long char contain space

Hi, i have to search for a char like that : export var1="i am not happy /not happy" with a command like : grep $var1 file but this not working with me !!! thank you in advance. (2 Replies)
Discussion started by: tizilfin
2 Replies

8. Shell Programming and Scripting

insert escape charactor within VIM

Hi, I need to send "^[" command to the buffer. I tried to use insert within VIM and press 'ctrl' key and then '^' and '[' key. but it didn't work. Does anyone know how to do it? Thanks a lot! Julie (2 Replies)
Discussion started by: cin2000
2 Replies

9. Shell Programming and Scripting

how to pick out last charactor of a string?

Hi, Does anyone know how many ways to pick out last charactor of a string? Thanks! (7 Replies)
Discussion started by: cin2000
7 Replies

10. Shell Programming and Scripting

Search with awk, but having space within

If in my search string with awk, if I have spaces, I am getting an awk error. e.g. awk /A B/ filename How can I search the pattern which has space within? (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question