Sponsored Content
Full Discussion: Sed append newline character
Top Forums Shell Programming and Scripting Sed append newline character Post 302209123 by Gaurang033 on Thursday 26th of June 2008 02:03:46 AM
Old 06-26-2008
Sed append newline character

Hi All,

I am new to Shell scripting.. I have a task to parse the text file into csv format. more then half the things has done.

But the problem is when I use the sed command in shell script. it appends newline character at the end of the line. and so when I open the file in CSV it's format is not as what I want..

I have the test file like
input.txt
test-1;1;1;100Mb/s
test-2;1;2;
test-1;2;2;
test-2;2;3;
test-2;3;4;

and I want to replace the data in the third column with some text ( it's in other file, info.txt) . And for that I am using the following shell script

info.txt
Result 1 Pass
Result 2 Fail
Result 3 Unknown
Result 4 Crash

Shell Script
Code:
exec < info.txt 
while read line; do 
	echo $line  > tmp 
	fnd=`awk '{print $2}' tmp`
	rep=`awk '{print $3}' tmp`
	echo "c=$fnd d=$rep"
        sed -i 's/test-[1-9],[1-9],'$fnd'/&'$rep'/' input.txt   
        sed -i 's/'$fnd$rep'/'$rep'/' input.txt
	          
done

After the above code I wish the input.txt file to be like below
test-1,1,Pass,100Mb/s
test-3,1,Fail,200Mb/s
test-1,3,Unknow,150Mb/s
test-1,3,Crash,150Mb/s

But when I open it in the text editor I found it like below Smilie
It appends extra newline character after the pattern find and replace


test-1,1,Pass
,100Mb/s
test-2,1,Fail
,
test-1,2,Fail
,
test-2,2,Unknown
,
test-2,3,Crash
Crash

And so when I export it to CSV file the format just ruins...


Please Please help me.
Thnx in advance ...

Last edited by Gaurang033; 06-26-2008 at 03:24 AM.. Reason: A minor mistake in the script posted
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

append newline to files with incomplete last line

Hi all, Is there any way I can check a file for the linefeed character at the end of the file, and append one only if it is missing (ie. Incomplete last line)? Need to do this because I need to write a script to process files FTP-ed over from various machines, which may or may not be... (1 Reply)
Discussion started by: ziyi
1 Replies

2. Shell Programming and Scripting

Using SED to append character to each line

Hey - my first post here, and I'm a total SED newb. I've looked around for previous help on this, but have so far been unsuccessful. I have a program (AMStracker for OS X) that outputs data in the terminal. Output is in this form: . . . 3 0 -75 3 0 -76 3 0 -77 ... (4 Replies)
Discussion started by: c0nn0r
4 Replies

3. Shell Programming and Scripting

append newline to existing variables

firstly, i check is the variable empty or not, if so vesselNameList=`echo $vesselName` if not vesselNameList="${vesselNameList}""\n"`echo "$vesselName"` and it produces this result BUNGA TERATAI 3 5055\ JADE TRADER 143W\ MOL SPLENDOR 0307A BUNGA TERATAI 3 5055\ JADE... (1 Reply)
Discussion started by: finalight
1 Replies

4. Shell Programming and Scripting

Append newline at the file end

Hi All, Is there any way to append a newline character at the end of a file(coma-separated file), through shell script? I need to check whether newline character exists at the end of a file, if it does not then append it. Regards, Krishna (1 Reply)
Discussion started by: KrishnaSaran
1 Replies

5. UNIX for Dummies Questions & Answers

newline character

hi, I want to print the below lines "Message from bac logistics The Confirmation File has not been received." When i give like this in the code "Message from bac logistics\n The Confirmation File has not been received." It is giving only Message from bac logistics\n The... (9 Replies)
Discussion started by: trichyselva
9 Replies

6. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

7. Shell Programming and Scripting

Why SED can't see the last newline character?

Removed. My question does not make sense. and SED does see the last newline character. But I still have a question: How to remove the last newline character(the newline character at the end of last line) using SED? ---------- Post updated 05-01-11 at 10:51 AM ---------- Previous update was... (7 Replies)
Discussion started by: kevintse
7 Replies

8. UNIX for Dummies Questions & Answers

newline character in a variable

variable="unix\nlinux" echo $variable expected output: unix linux :wall: can i do that ?? thanks in advance!! (3 Replies)
Discussion started by: sathish92
3 Replies

9. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

10. UNIX for Beginners Questions & Answers

Replace newline with comma and append quotes

Hi, I have below requirement. Apple Orange Banana Required O/p in bash 'Apple,Orange,Banana' Can you please help. Please wrap your samples, codes in CODE TAGS as per forum rules. (3 Replies)
Discussion started by: Rtk
3 Replies
App::Prove::State::Result(3pm)				 Perl Programmers Reference Guide			    App::Prove::State::Result(3pm)

NAME
App::Prove::State::Result - Individual test suite results. VERSION
Version 3.23 DESCRIPTION
The "prove" command supports a "--state" option that instructs it to store persistent state across runs. This module encapsulates the results for a single test suite run. SYNOPSIS
# Re-run failed tests $ prove --state=fail,save -rbv METHODS
Class Methods "new" my $result = App::Prove::State::Result->new({ generation => $generation, tests => \%tests, }); Returns a new "App::Prove::State::Result" instance. "state_version" Returns the current version of state storage. "test_class" Returns the name of the class used for tracking individual tests. This class should either subclass from "App::Prove::State::Result::Test" or provide an identical interface. "generation" Getter/setter for the "generation" of the test suite run. The first generation is 1 (one) and subsequent generations are 2, 3, etc. "last_run_time" Getter/setter for the time of the test suite run. "tests" Returns the tests for a given generation. This is a hashref or a hash, depending on context called. The keys to the hash are the individual test names and the value is a hashref with various interesting values. Each k/v pair might resemble something like this: 't/foo.t' => { elapsed => '0.0428488254547119', gen => '7', last_pass_time => '1219328376.07815', last_result => '0', last_run_time => '1219328376.07815', last_todo => '0', mtime => '1191708862', seq => '192', total_passes => '6', } "test" my $test = $result->test('t/customer/create.t'); Returns an individual "App::Prove::State::Result::Test" instance for the given test name (usually the filename). Will return a new "App::Prove::State::Result::Test" instance if the name is not found. "test_names" Returns an list of test names, sorted by run order. "remove" $result->remove($test_name); # remove the test my $test = $result->test($test_name); # fatal error Removes a given test from results. This is a no-op if the test name is not found. "num_tests" Returns the number of tests for a given test suite result. "raw" Returns a hashref of raw results, suitable for serialization by YAML. perl v5.16.2 2012-10-25 App::Prove::State::Result(3pm)
All times are GMT -4. The time now is 10:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy