LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-22-2011, 02:32 PM   #1
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Rep: Reputation: Disabled
Question command to write on first line of a file (as a heading for columns) in linux/solaris.


Hi,

i have file which is few line. i want to add some heading to the file in the first line. How can i?
 
Old 11-22-2011, 02:39 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,318

Rep: Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881
Quote:
Originally Posted by Sha_unix View Post
Hi,
i have file which is few line. i want to add some heading to the file in the first line. How can i?
You write a script to do it.

Echo your header to the file, then use the cat with the append operator ">>" to put the rest of the text into it, then use the mv (or cp) command(s) to get the file named what you want, and try to reference the man pages and Google scripting examples first.

Something like this:
Code:
echo "my header" > file1.txt
cat file-contents.txt >> file1.txt
mv file1.txt file-contents.txt
or use sed with inline-editing:
Code:
sed -i '1imy new header text is this' file-contents.txt

Last edited by TB0ne; 11-22-2011 at 02:46 PM.
 
1 members found this post helpful.
Old 11-22-2011, 02:52 PM   #3
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Hmmm k i can try that, but file looks like below. So if i have to seperate "," (comma) with space or tab. how can i do it.

quote:
"Newcomm_data",45,10486266,10148863
"Newcomm_index",46,3145741,1943543
"comm_data",47,8646650,7228927
"comm_index",48,2097166,1786591
"RUNLOG_Data_01",55,1311230,210943
[/quote]

i want to give heading like this

quote:
AreaName AreaNo TotalBlocks HiWaterMark
[/quote]
 
0 members found this post helpful.
Old 11-22-2011, 03:09 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,318

Rep: Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881
Quote:
Originally Posted by Sha_unix View Post
Hmmm k i can try that, but file looks like below. So if i have to seperate "," (comma) with space or tab. how can i do it.
Code:
"Newcomm_data",45,10486266,10148863
"Newcomm_index",46,3145741,1943543
"comm_data",47,8646650,7228927
"comm_index",48,2097166,1786591
"RUNLOG_Data_01",55,1311230,210943
i want to give heading like this
Code:
AreaName AreaNo TotalBlocks HiWaterMark
You were given the solution...how about trying it, and then trying to figure out how to make it work for your exact needs?? Read man pages...try Google. Coming back with each command, saying "it's not working!!!", shows little effort on your part, when you've been given the solution already.

Your request was for how to put a header into a file. I gave you two solutions that work. Want spaces in it? Put them in...and here's a hint: read your previous threads about replacing things, then think about what you did to get characters substituted. That was a backslash, right?? Think that would work here????
 
1 members found this post helpful.
Old 11-22-2011, 05:46 PM   #5
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Rep: Reputation: 45
Code:
#!/bin/sh

AreaName="$LANG"
AreaNo="`rdev | cut -f 1 -d ' '`"
TotalBlocks="`fdisk -l | grep "^$AreaNo" | tr -s ' ' | cut -f 4 -d ' '`"
HiWaterMark="$((TotalBlocks/100*90))"

echo "\"Newcomm_data\",45,10486266,10148863
\"Newcomm_index\",46,3145741,1943543
\"comm_data\",47,8646650,7228927
\"comm_index\",48,2097166,1786591
\"RUNLOG_Data_01\",55,1311230,210943
" > /tmp/test.txt

HEADER="#**************
#
# $AreaName $AreaNo $TotalBlocks $HiWaterMark
#
#*********************"

echo "$HEADER"
echo

sed -i "$HEADER" /tmp/test.txt

cat /tmp/test.txt
 
1 members found this post helpful.
Old 11-22-2011, 06:07 PM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by Karl Godt View Post
Code:
#!/bin/sh

AreaName="$LANG"
AreaNo="`rdev | cut -f 1 -d ' '`"
TotalBlocks="`fdisk -l | grep "^$AreaNo" | tr -s ' ' | cut -f 4 -d ' '`"
HiWaterMark="$((TotalBlocks/100*90))"

echo "\"Newcomm_data\",45,10486266,10148863
\"Newcomm_index\",46,3145741,1943543
\"comm_data\",47,8646650,7228927
\"comm_index\",48,2097166,1786591
\"RUNLOG_Data_01\",55,1311230,210943
" > /tmp/test.txt

HEADER="#**************
#
# $AreaName $AreaNo $TotalBlocks $HiWaterMark
#
#*********************"

echo "$HEADER"
echo

sed -i "$HEADER" /tmp/test.txt

cat /tmp/test.txt
You do know that providing the full answer, without explaining what everything does, will not help the OP learn how to do it on his/her own, right?
 
3 members found this post helpful.
Old 11-23-2011, 09:50 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
90. How do I prepend a text to a file (the opposite of >>)?

http://mywiki.wooledge.org/BashFAQ/090

Basically, this is the kind of question that gets asked, and answered, all the time. You really should get into the habit of doing a few web and forum searches first.
 
1 members found this post helpful.
Old 11-23-2011, 10:07 AM   #8
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by David the H. View Post
90. How do I prepend a text to a file (the opposite of >>)?

http://mywiki.wooledge.org/BashFAQ/090

Basically, this is the kind of question that gets asked, and answered, all the time. You really should get into the habit of doing a few web and forum searches first.
Just your two cents? Or would that be more like a whole dollar?
 
1 members found this post helpful.
Old 11-23-2011, 10:46 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Separating the fields with tabs, you will have columns that don't line up because some entries are too long, pushing the next column over to the next tab. You might want to look at using AWK with formatted printing, to control where each field should start.

Such as this:
Code:
AreaName           AreaNo TotalBlocks    HiWaterMark
----------------------------------------------------
"Newcomm_data"       45   10486266        10148863
"Newcomm_index"      46    3145741         1943543
"comm_data"          47    8646650         7228927
"comm_index"         48    2097166         1786591
"RUNLOG_Data_01"     55    1311230          210943
The header can be printed in the BEGIN clause. I produced this with a one line awk command.

Last edited by jschiwal; 11-23-2011 at 11:05 AM.
 
2 members found this post helpful.
Old 11-23-2011, 03:14 PM   #10
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Thank you very much all
 
  


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
how to write command line arguments as values, into a file which contains variables sathiawathi.m Linux - Newbie 2 07-17-2009 03:27 AM
shell script/command for converting columns/table onto a single line skuz_ball Programming 9 11-30-2007 04:02 AM
problem deleting file using solaris command line. szehanz Solaris / OpenSolaris 2 03-22-2005 08:52 PM


All times are GMT -5. The time now is 07:11 AM.

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