LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 03: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, 09:02 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 09:13 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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



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-20-2013 11:28 PM
Help Needed - How to count an item in a tab-delimited txt file? Jason7449 Linux - Newbie 3 03-07-2010 09:20 AM
searching pattern in tab delimited file using grep vaibhavs17 Programming 5 03-05-2010 01:02 AM
Parsing a tab delimited text file jajanes Programming 9 08-08-2003 10:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:11 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
Open Source Consulting | Domain Registration