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 02-16-2008, 06:27 AM   #1
chennaiguy
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
how to delete last number/word of a file and incude file count at the end of the


hi friends,

i have a file that includes file count appended at the end of the file, now am deleting few of the lines in the file but the file count remains the same as the older one.

So i need to append the modified file count in to the file.

Example of my file:

1|sdfsfsdf|somestuffs|nfkfkakf|0
1|sdfsfsdf|somestuffs|nfkfkakf|0
1|sdfsfsdf|somestuffs|nfkfkakf|0
1|sdfsfsdf|somestuffs|4->file count

After deleting one row :

1|sdfsfsdf|somestuffs|nfkfkakf|0
1|sdfsfsdf|somestuffs|nfkfkakf|0
1|sdfsfsdf|somestuffs|4

i need to change the file count to 3 instead of 4 using sheel script. Can someone please help me on this.
 
Old 02-16-2008, 06:44 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

I'm not sure if deleting one or more lines should also be done from this script.

The following will count the amount of lines in a file and adjust the number to that amount:
Code:
#!/bin/bash

linesInFile="`awk 'END{ print NR}' infile`"

sed -i '$s/[0-9][0-9]*$/'${linesInFile}'/' infile
Example run:
Quote:
$ cat infile
1|sdfsfsdf|somestuffs|nfkfkakf|0
1|sdfsfsdf|somestuffs|nfkfkakf|0
1|sdfsfsdf|somestuffs|4

$ ./testscript

$ cat infile
1|sdfsfsdf|somestuffs|nfkfkakf|0
1|sdfsfsdf|somestuffs|nfkfkakf|0
1|sdfsfsdf|somestuffs|3
The linesInFile part fills a variable with the actual amount of lines present in a file.
The sed part replaces the number present with the actual amount.

Hope this helps.
 
Old 02-18-2008, 10:08 PM   #3
chennaiguy
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Hi druuna,

This worked for me thanks a lot for your help.....

At first i got a error saying

illegal option -- i

Then instead of "-i" i used "-e" and saved in a new file, here is my command :

sed -e '$s/[0-9][0-9]*$/'${linesInFile}'/' infile > newfile

Last edited by chennaiguy; 02-18-2008 at 10:15 PM.
 
  


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
I want to make a link from word in one file to a different file using Ubuntu linux gilbert.baumslag Linux - Newbie 3 01-11-2008 02:32 PM
how can delete certain word from file Barq Linux - General 11 01-06-2008 08:06 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 02:45 AM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 05:41 PM
Count number of line in a file. philipina Programming 7 03-18-2004 06:04 PM


All times are GMT -5. The time now is 07:37 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration