Grep output problem

Thread Tools Search this Thread
# 1  
Grep output problem

Hi all,

Here is my problem:

I'm testing a login script for our mac Machines. This script output's in a csv file.

The login script looks like this:

echo "${computername}|${data1}|${data2}|${data3}" >> file.csv

The csv file looks like this:


Now I want to erase the data from computername2 because I only want to see the last output from every computernameX.

So before the login script output's the "Computername2|data1|data2|data3" line I want it to erase it's previous output.
When I use
grep -v Computername2 file.csv > file2.csv

the file2.csv is empty.

When I only use grep (without -v) the file2.csv is similar to file.csv.
When I manually edit the file.csv file en hit enter after all the lines and save the file then it works fine.
So somehow the grep command "sees" only one line. Now I searched the web for all day now but I can't figure out what to do. I think it has something to do with new lines or carriage return.

So what to do? Maybe use another command then grep? Or change the way of output in the login script?

Last edited by mattiasvdm; 11-28-2012 at 05:12 AM.. Reason: added code tag's
# 2  
perl -ne '@record=split/\|/;$record{$record[0]}=join "|", $_; _END_ {for (sort keys %record){print $record{$_};}' file.csv

# 3  
Originally Posted by Skrynesaver
perl -ne '@record=split/\|/;$record{$record[0]}=join "|", $_; _END_ {for (sort keys %record){print $record{$_};}' file.csv

Thanks for your reply.

The first error I got is:

Missing right curly or square bracket at -e line 1, at end of line
syntax error at -e line 1, at EOF
Execution of -e aborted due to compilation errors.

So I added the right curly bracket at the end like this:

perl -ne '@record=split/\|/;$record{$record[0]}=join "|", $_; _END_ {for (sort keys %record){print $record{$_}};}' file.csv

So then I get this error:

Can't call method "_END_" without a package or object reference at -e line 1, <> line 1.

I must say I have no experience with perl and I'm pretty new to unix as well. Still I wonder if there is a way to change the way of output of the initial data in the first place. Since the grep command actually works fine when I manually hit enter after all the lines.
# 4  
if grep -v Computername2 is not working, can you try awk instead:-
awk -F\| '$1!="Computername2"' file.csv

# 5  
Awk doesn't seem to work either. If I use:
awk -F\| '$1!="Computername2"' file.csv >> file2.csv

File2 is the same as file1. If I output to the terminal window it output's everything in one single line. So again that's where the problem seems to be.

---------- Post updated 29-11-12 at 12:00 PM ---------- Previous update was 28-11-12 at 04:02 PM ----------


The problem was in the first output. Every line ended with a Carriage Return (CR). That's why the grep command "sees" only one line of data. For the grep command to work every line has to end with a Carriage Return AND a Line Feed (CR+LF).

So what I did was change the way of output of the initial data:

echo "${computername}|${data1}|${data2}|${data3}\r" >> file.csv

As you can see I added the \r in the end. By doing this you get the CR and LF at the end of every line.

After this the grep command worked fine.

Thanks everyone for your help.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Hard
If you could fold a piece of paper in half 50 times, its' thickness will be 3/4th the distance from the Earth to the Sun.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep output

Hi. I have a command line that will generate exif data from .jpg files... So to get the Camera Model I use: find -iname "*.jpg" -print0 | xargs -0 exiftool -a | grep "Camera Model" But I want the file name AND the camera model... How to I get the grep to give me TWO bits of information from... (2 Replies)
Discussion started by: TuftyDave
2 Replies

2. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

3. UNIX for Dummies Questions & Answers

How to have the output in grep?

Hi guys - I am trying to have a certain file display information horizontally divided by a pipe. for example file name: foo.dat has information like this: name1 name2 name3 namenamename4 namene5 I would like it to display like this: name1|name2|name3|namenamename4|namene5 ... (6 Replies)
Discussion started by: DallasT
6 Replies

4. Shell Programming and Scripting

How to grep the desired output and output to a file?

Discussion started by: jediwannabe
10 Replies

5. Shell Programming and Scripting

grep output

hi i am running grep -c on a bunch of files some of which may be non existent. The output i get is in the same order as the files grep checks and this is what i want as i need to copy the output to a spreadsheet. However when i tried to pipe the output to sed to strip away the filenames and just... (5 Replies)
Discussion started by: chronics
5 Replies

6. Shell Programming and Scripting

grep output

From below mentioned line,i have to display output as last word only ie:arch_1_105366_720809746.dbf -rw-r----- 1 oracle dba 98887680 02 Mar 12:01 arch_1_105366_720809746.dbf Please .. (5 Replies)
Discussion started by: Sanal
5 Replies

7. UNIX for Dummies Questions & Answers

Help with grep output

Hello, I have a list of stings that I'm using with grep to determine which files contain the strings. Here's what I have now: list of strings in a file (list.txt): /directory/direc tory/file.jsp /dire ctory/directory/direct ory/file.jsp grep I'm doing to find files that contain the... (4 Replies)
Discussion started by: upstate_boy
4 Replies

8. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

9. Shell Programming and Scripting

GREP with contain output

How can I perform a grep and it will give me a contain word (maintenance) instead of a string or the whole line of info. grep with exclusive output? or is there a CONTAIN with IF statement ? I have a file call yast2_vhost.conf and it contain multiple entries call maintenance.html... (2 Replies)
Discussion started by: yoom@hostwebase
2 Replies

10. Shell Programming and Scripting

output of grep

hi, why the following is giving entire /etc/passwd file as output even when it does not contain ^ or $ . grep ' ' /etc/passwd whereas the following is not giving any output grep ' ^$ ' /etc/passwd (3 Replies)
Discussion started by: useless79
3 Replies

Featured Tech Videos