Is it possible to grep a certain amount of characters?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Is it possible to grep a certain amount of characters?
# 1  
Old 09-02-2011
Is it possible to grep a certain amount of characters?

I have a file similar to the following
filler filler filler
car 6 mazda
filler filler filler filler
car civic honda
car rav 4 toyota
filler filler


If i do a "grep -i car file.txt" the output would be
car 6 mazda
car civic honda
car rav 4 toyota


however, i want to have the first 9 characters of each line. I know that sort has a similar feature with +n, but I was wondering if i can do something similar to just get the following output:
car 6
car civic
car rav 4


I played with the cut command, the the field delimiter doesn't seem to work (since rav 4 contains a space).
# 2  
Old 09-02-2011
Quote:
i want to have the first 9 characters of each line
It is:
Code:
car 6 maz
car civic
car rav 4

So what do you really want?
# 3  
Old 09-02-2011
Code:
| cut -b-9

No need for delimiters if Youre writing bytes (chars).
This User Gave Thanks to sulti For This Post:
# 4  
Old 09-02-2011
Code:
 
$ nawk '/[Cc]ar/ {print substr($0,1,9)}' file.txt
car 6 maz
car civic
car rav 4

# 5  
Old 09-02-2011
Quote:
Originally Posted by sulti
Code:
| cut -b-9

No need for delimiters if Youre writing bytes (chars).
simple solution and worked like charm!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep with ... matching more than 3 characters

I am trying to understand what the grep command in ubuntu is trying to do here. The contents of my test file is given below harsha@harsha-H67MA-USB3-B3:~/Documents$ cat data abcd efghi jklmno pqr stuv wxyz When I grep for 3 dots (...) without the parenthesis as follows I would expect the... (4 Replies)
Discussion started by: sreeharshasn
4 Replies

2. Shell Programming and Scripting

grep particular characters help

Hello folks i have file which is below, i want to extract last column of file that contains dm-21 or dm-13 or dm-N it show output like I have tried but i got this (16 Replies)
Discussion started by: learnbash
16 Replies

3. UNIX Desktop Questions & Answers

grep a specific amount of occurrence

hey , i m trying to figure out how to do the following : i got a text file the looks like so: 1031 1031 1031 1031 1031 1031 1031 1031 16500 16500 16500 16500 1031 1031 (4 Replies)
Discussion started by: boaz733
4 Replies

4. Shell Programming and Scripting

Grep with special Characters

Need Help For GREP I have a file say g1.txt and content of file is below REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v NoDrives /t REG_DWORD /d 4 /f , REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v NoClose /t REG_DWORD /d 1 /f ,... (4 Replies)
Discussion started by: jalpasoni
4 Replies

5. Shell Programming and Scripting

Generate specific amount of same characters

Hi, Is there a command to print one character x amont of times? I need for example 10 comma's (,,,,,,,,,,). Instead of creating a loop, I was wondering if there is a way to do this with sed or awk? Thanks! (3 Replies)
Discussion started by: Subbeh
3 Replies

6. Shell Programming and Scripting

Grab characters following grep

Hi all, I'm trying to gather data directly following a keyword in a file but I have no guarantee where it will appear in said file so I can't use cut or anything else that assumes it will be located at a certain position. If anyone has any suggestions I'd be grateful. Here is an example of the... (7 Replies)
Discussion started by: Korn0474
7 Replies

7. UNIX for Dummies Questions & Answers

Using GREP for special characters

Hi folks I am issuing the following command: grep "" * Looking for the characters \/:*?"<>|#+%& within all files in a directory, but the command fails being unhappy with pipe: ksh: 0403-057 Syntax error: `|' is not expected. How do I force the command to take the pipe | ? I guess... (2 Replies)
Discussion started by: daveaasmith
2 Replies

8. UNIX Desktop Questions & Answers

grep with special characters

Hi there I need to grep for a detail from a file. The pattern to search for involves escape sequences in it. This causes for the problem. grep "P\_SOME\_STRING\_SEARCH" filename Note, I have line like below in the file and expect it to grep. select * from my_system_param ... (3 Replies)
Discussion started by: guruparan18
3 Replies

9. Shell Programming and Scripting

Grep with Special Characters

I need to sort a file, the sort is not a alphabetical sort, it's based on a predefined order which is read from a file called fSortOrder. The format of the fSortOrder file is : STARTPATH" .... .... The file that needs to be sorted is called tmpUnsorted and contains data in the format : ... (6 Replies)
Discussion started by: Vashj
6 Replies

10. UNIX for Dummies Questions & Answers

Grep and extract the characters after

Hi All, I have lines like below in a file A /u/ab/test1.dat A/u/ab/test2.dat A /u/bb/test3.dat A/u/cc/test4.dat I will need /u/ab/test1.dat /u/ab/test2.dat /u/bb/test3.dat /u/cc/test4.dat Pls help Thanks (6 Replies)
Discussion started by: thumsup9
6 Replies
Login or Register to Ask a Question