Using PERL to append chars to each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using PERL to append chars to each line
# 1  
Old 07-09-2006
Using PERL to append chars to each line

I tried using SED to do this, but I'm not having any luck with it. See the previous thread here.

I have a program called AMStracker (on OS X) that spits out the values of the motion sensor in the HDD. It has output that looks like this:

.
.
3 0 -75
3 0 -76
3 0 -77
.
.

I need to change it so that it looks like this:

.
.
3 0 -75;
3 0 -76;
3 0 -77;
.
.

ie. just add a ';' at the end of each line.

I was able to get the text _looking_ how I wanted with SED, but something was wrong that could not be seen. After filtering through SED I could no longer pipe the output into netcat (before filtering I could). I think that SED might be doing something to the line breaks?

Anyways, I tried using PERL instead of SED. I made the script below:

Code:
#!/usr/bin/perl

while (<>)  {
    $input = $_;
    $full = $input;
    printf("%s;\n", $full);
    }

But this gives me output like this:

.
.
;
23 0 12
;
22 0 13
.
.

Clearly I have the ';' before the '\n' in the script, but it does not appear that way in the output. All I can think of is that I need to strip the carriage return that comes with the input from AMStracker and then add my ';' and then replace the at the end of each line '\n'.

I suppose that using printf or sprintf will allow me to truncate the last byte of eac line that is coming in from AMStracker? How would I do this?

Help Please!
# 2  
Old 07-09-2006
May be it happens becouse AMStracker writes to stderr, try something like

/amstracker -u 0.05 -s | sed 's/.*/&;/' 2>&1 | nc 127.0.0.1 5001
# 3  
Old 07-09-2006
Well, first, no need start another thread on basically the same topic.

What exactly are you trying to accomplish? In the other thread, after someone posted a solution for your question, you added another kink to the issue. You need to explain the whole thing; otherwise, if you keep saying "oh this doesn't work because I have to do this also", I don't think you are going to get much help.

Now, being sed or perl, I think you are looking at the wrong side of the problem. Neither sed or perl are removing anything from the data. I think that the problem is with your usage of netcat.

To start, why are you trying to create a TCP connection to your localhost? That's what you are doing when you issue nc 127.0.0.1 5001. That makes no sense. You are already there.

So, explain exaclty what you are trying to do, from beginning to end.
# 4  
Old 07-11-2006
simple answer.

Goodness gracious. Please state everything known to man before I will provide an answer.

Simple answer for simple question. Try using chomp.

#!/usr/bin/perl

while (<>) {
$input = $_;
chomp($input);
$full = $input;
printf("%s;\n", $full);
}
# 5  
Old 07-11-2006
Quote:
Originally Posted by mugg
Goodness gracious. Please state everything known to man before I will provide an answer.

Simple answer for simple question. Try using chomp.

#!/usr/bin/perl

while (<>) {
$input = $_;
chomp($input);
$full = $input;
printf("%s;\n", $full);
}
Goodness gracious. Why don't you read what was posted. This guy has started 3 threads on the same topic. The substitution of the character is not his issue. He thinks that is his issue, but it isn't. See, goodness gracious? His problem is that he's not seeing what he wants at the end of a command in which he gets a string of characters that then is piped into netcat. Ah! guess you need to know something more than what was posted. Go back and read the original thread he posted, goodness gracious.

BTW, goodness gracious, what you posted is not the simple answer. It's about 6 lines too long.
# 6  
Old 07-11-2006
just a friendly reminder from the moderator.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep with special chars line by line

I have a file that includes strings with special characters, eg file1 line: 1 - special 1 line: = 4 line; -3 etc How can I grep the lines of file1 from file2, line by line? I used fgrep and egrep to grep a particular line and worked fine, but when I used: cat file1|while read line;do... (2 Replies)
Discussion started by: FelipeAd
2 Replies

2. Shell Programming and Scripting

Perl search and append new line

Dear All, I want search two lines and append some string in between these lines. Input file tmp,123 ,10:123 tmp,666 ,50:999 tmp,2:19800 5,3:21. tmp,2:19800 55555555 tmp,2:19800 5,3:21.Output should be tmp,123 ,10:123 tmp,666 ,50:999 tmp,2:19800 (4 Replies)
Discussion started by: arvindng
4 Replies

3. Shell Programming and Scripting

how to append a string to next line in perl

hi all , i have a requirement like this.. this just a sample script... $ cat test.sh #!/bin/bash perl -e ' open(IN,"addrss"); open(out,">>addrss"); @newval; while (<IN>) { @col_val=split(/:/); if ($.==1) { for($i=0;$i<=$#col_val;$i++) { ... (2 Replies)
Discussion started by: tprayush
2 Replies

4. Shell Programming and Scripting

why am I losing line-end chars

Hello, I do not know why the output from these two methods differs. One method retains the newlines, the other method appears to ignore or lose the newlines. Writing a file with the redirection operator: egrep -e 'matchstring' infile.txt > outfile.txt The resulting outfile.txt contains... (3 Replies)
Discussion started by: duderonomy
3 Replies

5. Shell Programming and Scripting

sort file with non ascii chars and cjk with perl

Hello, I am not a programmer, please be patient. Actually, I have started to look into Perl because it seems to be able to solve all the problems (or most of them) I happen meet using my computer. These problems are generally all text-manipulation-related. Although I started to study, I cannot... (6 Replies)
Discussion started by: ahsog
6 Replies

6. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies

7. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

8. Shell Programming and Scripting

insert new line at found chars

Hey gang, I have: XXZZXXZZXX 123 asdaffggh dfghyrgr ertyhdhh XXZZXXZZXX 234 sdg XXZZXXZZXX 456 gfg fggfd That is all on one line. Very simply put I want to do is something like: sed s'/XXZZXXZZXX /\n/g' or tr 'XXZZXXZZXX ' '/n' I have tried various things but can never get the desired... (6 Replies)
Discussion started by: crowman
6 Replies

9. Shell Programming and Scripting

Get multiple line content between two chars

Hello - I have a file that has the something like the following : REM CREATE TABLE lots of text REM table specifc creation text ; REM ALTER TABLE lots of text REM text specific to the the alter command REM could be more lines of text; What I need is to get all the lines for the ALTER... (2 Replies)
Discussion started by: Feliz
2 Replies

10. Shell Programming and Scripting

to append few chars at the end of a file

hi i want to open a file at runtime append few chars at the end of each line all these i want to have done automatically how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies
Login or Register to Ask a Question