LinuxQuestions.org
Review your favorite Linux distribution.
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 09-17-2009, 12:20 AM   #1
LarsB
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
script to insert no. of lines in a file to the start of the file


Hey all

I'm looking for a way to insert the number of lines in a file to the start of the aformentioned file. This should be simple but as I am not used to scripts in Linux, I am finding it tough going.

I can find the number of lines in a file easily enough via

filesize=$(awk 'END {print NR}' $1)

but as for inserting this into the first line, i'm failing to do so. I've tried some of the other approaches on these forums but none so far have been able to do so.

I've tried:
sed '1i\$filesize' $1
but sed i requires a string, not a variable so no go

I've also tried:
mv "$1" "${1}.bak" 2>/dev/null || touch "${1}.bak"
cat $filesize "${1}.bak" >"$1"

but again with no luck as cat seems to need an input stream

Would someone be able to illuminate me with a good way to do so?

Just to recap, i want to insert a line at the start of a given file that holds the number of lines the original file has.

ie the file:
a
b
c
d
e

should become:
5
a
b
c
d
e

Thanks in advance
 
Old 09-17-2009, 12:22 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
num=$(wc -l $filename)
echo $num > $newfile
cat $filename >> $newfile
 
Old 09-17-2009, 12:49 AM   #3
LarsB
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Hmmm, it is almost exactly what i need, thank you for the quick response. Much appreciated

my only issue is it also prints the name of the file as well as num=$(wc -l $filename) outputs lines as well as filename

In case anyone else is interested, i changed it slightly to this. It creates a backup of the original while appending the original file with the number of lines at start (note: it only appends number of lines, nothing else)


num=$(awk 'END {print NR}' $filename)
cp $filename ${filename}.bak

echo $num > $filename
cat ${filename}.bak >> $filename


thanks again for the help

Lars
 
Old 09-17-2009, 08:22 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Yeah, sorry, was doing it off the top of my head. You can just amend the 1st line
Code:
num=$(wc -l $filename| awk '{print $1}')
 
  


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
Basic bash script question re: file size or # of lines in a file the_fornicator Programming 6 09-03-2009 09:41 AM
shell script: insert line in a file noir911 Programming 6 02-04-2008 10:42 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
Random file lines directed to a new file. In script an error. In command line no err leventis Programming 1 09-28-2006 07:16 AM
Script to insert string in first line of a file minil Programming 13 01-02-2006 11:56 PM

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

All times are GMT -5. The time now is 03:24 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