LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: 0
Create tab delimited file with header


Hi,

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:
Code:
#!/bin/bash

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

HEADER="Environment\tNode"

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

for DIR in $DIRS
do
echo  ${DIR}
NFILE="/opt/nodes/$DIR/config"

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

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

echo "###### END OF SCRIPT ######"
Above piece of code:
Will read node.xml file. Look for props. assign variables as below:
e.g.,
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

Problms:
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.
Thanks,
 
Old 10-29-2015, 10:02 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

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

# OR

>/usr/node.txt
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
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,040

Rep: Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914
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.
 
  


Reply


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


All times are GMT -5. The time now is 04:39 PM.

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