Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-29-2015, 04:54 AM   #1
LQ Newbie
Registered: Dec 2007
Posts: 14

Rep: Reputation: 0
Create tab delimited file with header


I have requirement to create tab delimited file with values coming from variables.
1. File will contain only two columns separated by tab.
2. Header will be added once.
3. Values will be keep adding upon the script run.
4. If values already exists then values will be replaced.

I have done so far below work:

DIRS=`ls -l $NDIR | egrep '^d' | awk '{print $9}'`


printf "1\ni\n$HEADER \n.\n\nw\nq\n"|ed -s /usr/node.txt

for DIR in $DIRS
echo  ${DIR}

ENV=$(sed -n '/<user-props>/,\|</user-props>| {
   s/ *<kv-pair name="" value="\([^"]*\)"\/>/\1/p
   }' $NFILE/node.xml)
NODE=$(sed -n '/<sys-props>/,\|</sys-props>| {
   s/ *<kv-pair name="" value="\([^"]*\)"\/>/\1/p
   }' $NFILE/node.xml)

echo -e "$ENV\t$NODE" >> /usr/node.txt

echo "###### END OF SCRIPT ######"
Above piece of code:
Will read node.xml file. Look for props. assign variables as below:
ENV = SystemEnv
NODE = SystemNode
There are multiple Nodes in nodes folder so for loop to take care.

Currently I'am able to write values into output file node.txt

First time script will run ok. File will be perfect.
But Next time on run Header will be added again and if the folder contains same node names those will be added.
If I use only ">" method with echo then only last node will be added, ignoring the others.
Header method I used to add always print
upon run. I couldn't figure out why.

1. Is there any other method to add header in tab delimited style.
2. Shall use sed or awk instead of echo to make output file?
3. Replace the existing entries.

help needed to make it working as per needs.
Old 10-29-2015, 10:02 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
If its ok to replace the entire file on each run, then either
rm /usr/node.txt

# OR

at the start of the run.

IF it needs to be created only once, then 'updated' on each run, I personally would use eg Perl instead of bash etc.
One of Perl's basic var types is a hash, which is ideal for ensuring uniqueness of records.
Old 10-29-2015, 10:13 PM   #3
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,951

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Totally agree. But if it has to be bash ...
The reason you get multiple headers is because you tell "ed" to insert the header at line 1 - every time. Use "echo -e" like you do later - bash has several test that would help; maybe a simple "if [ !-e /usr/node.txt ]" would suffice.
As for multiple entries, that of course will happen if you do nothing to interrogate the /usr/node/txt - your script simple appends everything it finds.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing sar output text file, tab delimited LymanYucius Programming 3 02-21-2013 12:28 AM
Help Needed - How to count an item in a tab-delimited txt file? Jason7449 Linux - Newbie 3 03-07-2010 10:20 AM
searching pattern in tab delimited file using grep vaibhavs17 Programming 5 03-05-2010 02:02 AM
Parsing a tab delimited text file jajanes Programming 9 08-08-2003 11:34 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:16 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration