Help with grep and regex


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with grep and regex
# 1  
Old 03-25-2008
Help with grep and regex

Hi all,

I'm a beginner with linux, regex, grep, etc
I am trying to get data out of a file that has about 13,000 lines in this format

name - location

I want to grep all the names out to one file and the locations to another so I can put them into a spreadsheet.

Some have hyphenated names or hyphenated locations as well to complicate things.

The way I was going to approach it was to first grep out the lines with only one hypen to one file and all the ones with multiple hyphens to another.

Then I need an expression to select all data on one side of the hyphen and place it into one file, and the data on the other side of the hyphen into another file.

I'm going through all the tutorials I can find, but they don't read much better than man pages and I can't figure out how to do what I want. Can anyone help me get started with this?


Thanks in advance!
# 2  
Old 03-25-2008
Code:
sed -e 's/ - /	/'

That's a tab behind the second slash. Assuming you have a space on both sides of the divider dash, and not too many dashes with spaces on both sides which are not dividers, this should pretty much solve the issue. Probably your spreadsheet (yuck) can even handle tab-delimited input.

Next up, read the manual for the "cut" command.
# 3  
Old 03-25-2008
well that removed all the hyphens between name and location WITHOUT removing the words that needed to be hyphenated, so that's a start, but it still doesn't allow me to separate the data on either side of the delimiting hyphen... Unless I did something wrong.. I typed:

sed -e 's/ - / /' list > list2

Thanks!

Oh wait, I see where you were going...

The problem now is that there are spaces in names and locations, and this appears to have placed a space between the data. Not enough to separate the data logically, at least for Calc.
# 4  
Old 03-25-2008
I changed the command to read
sed -e 's/ - / % /' list > list3

and now I have a separate symbol to break up the data. Is there an expression to say cut all data up to the % and write to a file?
# 5  
Old 03-25-2008
got it, thanks.

The answer was to use cut as such:

cut --delimiter='%' -f 1 list3 > name
cut --delimiter='@' -f 2 list3 > location

Thanks for the help!
# 6  
Old 03-26-2008
A tab is a more conventional separator character, although of course it looks like just whitespace.

To type a tab at the command line, you often need to use some escape code, like ctrl-v tab to get a literal tab.
# 7  
Old 04-14-2008
sigh... still working on this.

There are 2 more things I have to figure out. .

How do you indicate all lines beginning with specific file paths for example:

A/A/*

sed -e 's/"A/A/*"/ /g' > newfile

doesn't seem like it's going to work...

And how do I indicate a blank line?
 
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 regex

Hi everyone, I'm looking for a grep command to match the following pattern from a file: <EGS>10234567<EGS> I used this following command to do this: grep -E '^<EGS>{8}<EGS>' test.txt In output I got: <EGS>10234567<EGS> Till now it work, but if I add something at the end of the line... (2 Replies)
Discussion started by: Arnaudh78
2 Replies

2. UNIX for Beginners Questions & Answers

Grep in regex

Hello guys, Here i am writing a script in bash to check for a valid URL from a file using regex This is my input file http://www.yahoo.commmmmm http://www.google.com https://www.gooogle.co www.test6.co.in www.gmail.com www.google.co htt://www.money.com http://eeeess.google.com... (2 Replies)
Discussion started by: Meeran Rizvi
2 Replies

3. Shell Programming and Scripting

Grep with regex containing one string but not the other

Hi to you all, I'm just struggling with a regex problem and I'm pretty sure that I'm missing sth obvious... :confused: I need a regex to feed my grep in order to find lines that contain one string but not the other. Here's the data example: 2015-04-08 19:04:55,926|xxxxxxxxxx| ... (11 Replies)
Discussion started by: stresing
11 Replies

4. Shell Programming and Scripting

grep -v and regex

How to match lines that don't contain a patern in regex it self, without using the -v option of grep? (15 Replies)
Discussion started by: vistastar
15 Replies

5. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

6. Shell Programming and Scripting

regex and grep

I want it to find lines that contain any number of capital letters before P this is what I have tried echo "AAAAAP" | grep 'P' echo "AAAAAP" | grep '\{1\}P' echo "AAAAAP" | grep '^*P' But none of them seem to work, any help is much appreciated thanks Calypso (4 Replies)
Discussion started by: Calypso
4 Replies

7. Shell Programming and Scripting

grep and regex question

basically i have a csv i parse through. a user will supply me with a san switch he/she wants more info about... say the name is "pnj-sansw124" now i can grep out every connection to that switch w/o issue because this sans switch pnj-sansw124 has multiple slots 1-10. and it looks like this in the... (5 Replies)
Discussion started by: pupp
5 Replies

8. UNIX for Dummies Questions & Answers

grep with Regex help!

Hello everybody, I'd like to know how is it I should write a regex in unix to match a string not followed by another string (anywhere in the line). To be more specific, I want to find lines where "drop table" is found, but not followed anywhere in the line by the character "&". For... (3 Replies)
Discussion started by: mvalonso
3 Replies

9. Shell Programming and Scripting

grep regex problem

Hi, I am trying to do something with grep, but for some reason I just can't get it to to work. I am looking for find a match in the second field, the length must be 10 characters and end with 'abc'. The file is in this format: <int><tab><field2> I've tried a few patterns, some work,... (2 Replies)
Discussion started by: iceman
2 Replies

10. UNIX for Dummies Questions & Answers

use of regex on grep

having a look on the regex site I saw that characters can be search using hex values http://www.regular-expressions.info/characters.html So I try to use it whith grep to find a è on a string (octal Decimal Hexa : 350 232 E8) but it doesn't work E.g. /usr/bin/echo '\0350' | egrep '\xE8' ... (0 Replies)
Discussion started by: solea
0 Replies
Login or Register to Ask a Question