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 11-22-2011, 01: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, 01:39 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
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 01:46 PM.
 
1 members found this post helpful.
Old 11-22-2011, 01: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, 02:09 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
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, 04: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, 05:07 PM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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, 08:50 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 09:07 AM   #8
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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, 09:46 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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 10:05 AM.
 
2 members found this post helpful.
Old 11-23-2011, 02:14 PM   #10
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

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


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

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

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