How to append a value to the output after using sed command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to append a value to the output after using sed command?
# 1  
Old 06-10-2013
How to append a value to the output after using sed command?

Hi All,

I have a file where I am converting newlines to comma separated values but I would like to append zero if the output is empty

Here is the command I am using
Code:
sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' test1.txt

test1.txt will have comma seperated values but sometimes this file can be empty so I wanted to append 0 to this file how could I do this.

I appreciate your help.

Regards,
raj

Last edited by Franklin52; 06-11-2013 at 08:22 AM.. Reason: Please use code tags
# 2  
Old 06-10-2013
I am not clear what you are asking, but maybe this will be helpful to you:
bash has -s test for non-empty files,
Code:
if [ ! -s empty_file ]; then echo "EMPTY"; else echo "Not Empty"; fi

So you could do
Code:
echo "0" > empty_file

if the file is empty.
# 3  
Old 06-10-2013
I wanted to do like this echo "0" >> sed command please let me know if this works and output file should have 0 in it. how do i do this approach

Regards,
raj
# 4  
Old 06-10-2013
What @Migurus told is the easiest way!
# 5  
Old 06-10-2013
Another approach using awk:
Code:
awk 'NF{s = s ? s OFS $0 : $0; f = 1 }END{ if (f) print s; else print "0" }' OFS=, test1.txt

# 6  
Old 06-11-2013
Hi Yoda,

I tried using awk command but its just showing as 0 at unix command prompt but not appended to the file
Code:
awk 'NF{s = s ? s OFS $0 : $0; f = 1 }END{ if (f) print s; else print "0" }' OFS=, test1.txt

Please let me know if I am wrong somewhere

Thanks
Raj

Last edited by Franklin52; 06-11-2013 at 08:22 AM.. Reason: Please use code tags
# 7  
Old 06-11-2013
Me too, I'm not clear what you want to achieve. Do you want the original file to contain a single "0" char after the operation if before it is empty?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. Shell Programming and Scripting

sed command to append word at end of line

hello Team, I am looking for sed command or script which will append word at end of line. for example. I want to validate particular filesystem with mount |<filesystem name> command. if nodev parameter is not there then it should add in the fstab file with receptive to the filesystem. # mount... (8 Replies)
Discussion started by: ghpradeep
8 Replies

3. UNIX for Dummies Questions & Answers

sed Command Output

I want to know how this command output will be sed 's/^"\(.*\)"$/\1/' <filename> (3 Replies)
Discussion started by: abhilashnair
3 Replies

4. Shell Programming and Scripting

sed command not getting the right output

Hello. Ive spent probably a hour on this problem, and cant figure it out. Let me explain the problem. I run head -n5 datasets/q13data.txt and get this : $$001011<-:::$$<-::: ' GreenWHITE<-::3.1415<-:::"BLACK fubar<-:::phi<-:::foochi $$$Yellow->:::'<-:::VOIDTue taochi->::$->:::Blue"... (12 Replies)
Discussion started by: jozo95
12 Replies

5. UNIX for Dummies Questions & Answers

Output of sed command to another sed command

Hi All, I'm relatively new to Unix scripting and am trying to get my head around piping. I'm trying to take a header record from one file and prepend it to another file. I've done this by creating several temp files but i'm wondering if there is a cleaner way to do this. I'm thinking... (10 Replies)
Discussion started by: BigCroyd
10 Replies

6. Shell Programming and Scripting

awk - append date to output of a command

Hi all; I am running a script:/var/tmp/gcsw -ne | grep "State:2" | wc that gives me output like:80 480 6529 but i need this output as:2013-01-18 13:00 -> 80 480 6529 (1 Reply)
Discussion started by: gc_sw
1 Replies

7. Shell Programming and Scripting

How to use variables in 'sed' append command?

HELLO!! I'm trying to pass a variable with in the 'sed' command (which would add some piece of code to file at a particular line). We can use sed '{line-number}a\ alfjaljf\ aslfjsfsjafl\ adlfjaf\' file.txt If file.txt is Now, I would like to add the parameter 'lmn' after... (1 Reply)
Discussion started by: mjavalkar
1 Replies

8. Shell Programming and Scripting

How to append something to a word using sed command

Hi, How to append something to already existing word. Suppose, I have the following line as a part of a file. VVV= jdbc:... (6 Replies)
Discussion started by: Dpu
6 Replies

9. Shell Programming and Scripting

Help with tar --append command output redirection

I am using tar command to append daily database backups on tape. "tar --append " command help me to do this. But tar --append command does not produce any output on stdout if it succeed. I want the output for that appended command to a log file. This log file should contain only the name of the... (0 Replies)
Discussion started by: pganguly46
0 Replies

10. Shell Programming and Scripting

What's wrong with this sed command? delete & append

I want to write a sed command that does the following work: file: <a>asdfasdf<\s> <line>hello</line> <b>adf<\c> <b>tttttttt<\c> output: name=hello sed -e 's/^*//' -n -e '/<line>/s/<*>//gp;' -e 's/^/name="/g' file but I can not append "=" after getting the line with... (5 Replies)
Discussion started by: minifish
5 Replies
Login or Register to Ask a Question