conditional append one line in file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conditional append one line in file.
# 1  
Old 04-20-2012
conditional append one line in file.

Hi, Unix gurus,

I have a requirement as following:
checking existing file, if the file only contain one line. then append "No data" else keep existing file as is.
can i achieve this by in command line without write a script.

Smilie

Thanks in advance.
ken002
# 2  
Old 04-20-2012
may be

Code:
NR=`wc -l yourfile`
if [ $NR -eq 1 ]; then
   echo no data >> yourfile
fi

corrected $ before NR.
Moderator's Comments:
Mod Comment code tags for code, please.

Last edited by ajayyadavmca; 04-20-2012 at 04:05 PM..
# 3  
Old 04-20-2012
The above code won't work because of a missing $ in front of NR...

The difference between code in a script and code not in a script is only where you type it.


Code:
[ $(wc -l < filename) -eq 1 ] && echo "No data" >> filename

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 04-20-2012
Quote:
Originally Posted by Corona688
The above code won't work because of a missing $ in front of NR...

The difference between code in a script and code not in a script is only where you type it.


Code:
[ $(wc -l < filename) -eq 1 ] && echo "No data" >> filename

It works!!!
Thanks
This User Gave Thanks to For This Post:
ken002
# 5  
Old 04-20-2012
Try an awkie:
Code:
awk 'END{ if(NR==1) print "No data" >> FILENAME }' file

--edit--
This should be more efficient, it only reads 2 lines of the file:
Code:
awk 'getline{exit} {close(FILENAME); print "No data" >> FILENAME }' infile


Last edited by Scrutinizer; 04-20-2012 at 04:46 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to append to array within conditional block in ksh/korn shell?

Hi, I have one array created and some values are there in ksh. I want to append some other values to it based on some condition in if statement. #!/bin/ksh echo "---------------------------------------------------" set -A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}'... (2 Replies)
Discussion started by: sanzee007
2 Replies

2. Shell Programming and Scripting

Append string to first line of a file

Hi, Please suggest me to write unix command, HEADER20110101 string append to first line of a file.. Regards Akshu (3 Replies)
Discussion started by: akshu.agni
3 Replies

3. Shell Programming and Scripting

How to append text to the second line of a file

Say I have a text file like: 1 3 4 How would I use ksh to put the number '2' into the second line of that file? I'm using OpenBSD so the sed syntax might be a bit different (I have no idea how to use sed, though) (4 Replies)
Discussion started by: guitarscn
4 Replies

4. Shell Programming and Scripting

append a line into a file in the top

hi, My code is #!/bin/sh echo "\n\nPlease enter the month of the year(YYYYMM) : \c" read date_rep INPUT_L9_FILE=L9_Recharge_Description_EOM_$date_rep.csv #This part is used to summarise Grand_Total, Balance_Total of file L9_Recharge_Description_EOM_${1}.csv. awk -F"," '{if(NR!=1)... (5 Replies)
Discussion started by: madfox
5 Replies

5. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

6. Shell Programming and Scripting

Append each line to next previous line in a file

Hi all, Please help me in providing sample code to append the following 4 lines in one row. Input : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 RXOCF-430 HY1711 1 EXTERNAL ALARM DOOR ALARM Output should be : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 ... (8 Replies)
Discussion started by: sudhakaryadav
8 Replies

7. Shell Programming and Scripting

how to append line of of data to file

hai..i am new to unix..and i've currently learn shell script.. i have this small problem where i would like to save every data from log file into user directory if the data is equal to the name of the user.. i manage to do that with below script.. i would like to ask if there is any solutions so... (1 Reply)
Discussion started by: meggae
1 Replies

8. Shell Programming and Scripting

Conditional reverse of certain file line order

Hi I am brand new to programming, I dont know how to go about this task, or what language is best for this...If there is an easy solution in different languages, I would love to see them. I want to learn about the steps to take on this, so Please put in comments where code is used. I believe in... (9 Replies)
Discussion started by: perlrookie
9 Replies

9. UNIX for Dummies Questions & Answers

How to Append a Value to each line of the file

Hi, We have Multiple source files which has some data, I need a K shell script which will append number '1' as the last character to all the lines of the first file and then increments the value and appends '2' to all the lines to the next file and so on. For Example Incoming file 1 ... (11 Replies)
Discussion started by: dsshishya
11 Replies

10. UNIX for Dummies Questions & Answers

Large file need to append to each line

I have a few large files that need to have a ,A appended to the end of each record. I though about using sed but never used it before and the man is not intuitive nor have I found examples. This is what I tried for file name bob sed '\a,A' bob from what I get if you do not supply a range... (1 Reply)
Discussion started by: r1500
1 Replies
Login or Register to Ask a Question