The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to convert byteArray variables to HexaString variables for Linux? ritesh_163 High Level Programming 2 08-11-2008 12:55 AM
naming variables with variables Allasso Shell Programming and Scripting 2 06-27-2008 11:45 AM
variables DNAx86 Shell Programming and Scripting 3 04-17-2008 05:36 PM
Regarding PS1,PS2,PS3,PS4 variables. anchal_khare Shell Programming and Scripting 5 02-04-2008 04:29 AM
Using sed with variables Mark_A_Tritz UNIX for Dummies Questions & Answers 2 04-28-2004 09:46 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-16-2008
Mr_Plow Mr_Plow is offline
Registered User
  
 

Join Date: Sep 2008
Location: Perth, Scotland
Posts: 8
Using sed with variables (again!)

Hi,

I'm trying to use sed to delete the last three lines of a file. I currently have:


Code:
# get the amount of lines in the file
foldernum=`wc -l File_In.txt | cut -c1-8`
# remove the lines in the file
sed "${foldernum}-3,${foldernum}d" File_In.txt > File_Out.txt

I get the error - sed: unknown command

Is there a better way or have I simply got my syntax incorrect.

Cheers.
  #2 (permalink)  
Old 09-16-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,350
Another way:


Code:
awk -v var=`wc -l < File_In.txt` 'NR==var-2{exit}1' File_In.txt > File_Out.txt

Regards
  #3 (permalink)  
Old 09-16-2008
Mr_Plow Mr_Plow is offline
Registered User
  
 

Join Date: Sep 2008
Location: Perth, Scotland
Posts: 8
Quote:
Originally Posted by Franklin52 View Post
Another way:


Code:
awk -v var=`wc -l < File_In.txt` 'NR==var-2{exit}1' File_In.txt > File_Out.txt

Regards
I tried the code above but it didn't trim the last 3 lines of the file.
  #4 (permalink)  
Old 09-16-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,350
Quote:
Originally Posted by Mr_Plow View Post
I tried the code above but it didn't trim the last 3 lines of the file.
It works fine for me, maybe you should use gawk, nawk or /usr/xpg4/bin/awk on Solaris as era mentioned.

Regards
  #5 (permalink)  
Old 09-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You need to do the arithmetic separately, sed doesn't know how to subtract. awk does know; do you get an error message or does it just not do what you want? Maybe you need mawk/nawk/gawk/XPG4 awk if your default awk is a really old "traditional" awk.


Code:
foldernum=`wc -l <File_In.txt`  # note use of redirection
limit=`expr $foldernum - 3`
sed -e "$limit",'$d' File_In.txt >File_Out.txt

  #6 (permalink)  
Old 09-16-2008
Mr_Plow Mr_Plow is offline
Registered User
  
 

Join Date: Sep 2008
Location: Perth, Scotland
Posts: 8
Quote:
Originally Posted by era View Post
You need to do the arithmetic separately, sed doesn't know how to subtract. awk does know; do you get an error message or does it just not do what you want? Maybe you need mawk/nawk/gawk/XPG4 awk if your default awk is a really old "traditional" awk.


Code:
foldernum=`wc -l <File_In.txt`  # note use of redirection
limit=`expr $foldernum - 3`
sed -e "$limit",'$d' File_In.txt >File_Out.txt
For the awk command I didn't get an error message but it didn't do what I wanted.

However, I've played around with the code you provided and I got it to work accordingly.

Thanks.
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0