trim lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trim lines
# 1  
Old 03-26-2008
trim lines

I want to delete all lines from 1 till a certain line.

how to do that without redirection to another file (tail solution)?

thanx
# 2  
Old 03-26-2008
With GNU sed:

Code:
sed -i 1,nd file

n = number of lines

With perl:

Code:
perl -i -ne'print unless 1..n' file

# 3  
Old 03-26-2008
Use head

Code:
head -5 filename

gives first 5 lines of the file filename.

If you want to keep the nr of lines as a variable, you can use command:
Code:
head -n $nr_of_lines_reqd filename

# 4  
Old 03-26-2008
Hi and thanks for your replies.
Actually I was not clear in my first post
the output of the perl/sed/head will be displayed on stdout. I expect the result to be in the file itself and not on the stdout. i.e. the result will should be a shorter file (without creating a new file)

thanks again.
# 5  
Old 03-26-2008
Melanie, what you want doesn't exist, at least not in standard UNIX. Every utility (sed, awk, ...) will create a separate file and if you want to replace your file with the trimmed one you will have to "mv" it.

Th GNU sed does indeed have the "-i" option allowing for in-place editing, but even then a temporary file (probably somewhere in /var/tmp) will be created by the tool. The same is true for editors like vi, ed, etc..

As there is no difference in principle where this temporary file is created that means that all these utilities work the same way.

If you want to preserve the i-node number of the file in question do the following: create a intermediate file and then use "cat" to overwrite the old file:

Code:
# head -${some_number} file > newfile
# cat newfile > file
# rm -f newfile

This will make sure that "file" will have the same i-node number before and after the change.

I hope this helps.

bakunin
# 6  
Old 03-26-2008
You have to redirect to another file. awk or sed can be used.

An awk solution...
Code:
awk '{if (NR<3) print}' file.dat


Well, in that case a simple head can also be used to get the output to another file.

I guess we cannot trim the file without creating another file.
# 7  
Old 03-26-2008
Quote:
Originally Posted by melanie_pfefer
Hi and thanks for your replies.
Actually I was not clear in my first post
the output of the perl/sed/head will be displayed on stdout. I expect the result to be in the file itself and not on the stdout. i.e. the result will should be a shorter file (without creating a new file)
[...]
You were clear,
but you didn't try the commands I suggested Smilie

And, of course, bakunin is rigth,
these commands use temporary files implicitly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim Space

In Shell, I have output of a unix command as test1 test2015 but I want it as test1 test2015 can anyone help me out. Use code tags, thanks. (3 Replies)
Discussion started by: OscarS
3 Replies

2. Shell Programming and Scripting

Trim the data

i am passing below inputfils using while loop and ouput should be trim as below INPUT: src_SPS_d_Comment_Tran_File_20130417_001.dat src_SPS_d_Corp_Adv_Tran_File_20130417_001.dat src_SPS_d_Letter_Tran_File_20130417_001.dat src_SPS_d_Loan_Level_File_20130417_001.dat... (8 Replies)
Discussion started by: katakamvivek
8 Replies

3. Shell Programming and Scripting

Trim String

Hi, I wish to grep everything before the last "/bin" in the following string /opt/app/bin/app1/jdk150_07/bin/IA64N/java Desired output: "/opt/app/bin/app1/jdk150_07" Kindly help ... (2 Replies)
Discussion started by: mohtashims
2 Replies

4. Shell Programming and Scripting

Trim spaces

All, i am comparing the output of one command to a numberic if ] but my problem is the output of follwoing is but but has some leading columns. I don't have any problme in LINUX and HP-UX. But only in AIX i am getting the leading spaces. I have developed my script on LINUX but when... (4 Replies)
Discussion started by: rcc50886
4 Replies

5. Shell Programming and Scripting

Help with awk trim

I am trying to trim spaces for the fixed width file starting from location 129 and of length 20. I am expecting only around 40 records that will have length greater than 9. But i am getting around 4000 records. Please help me correct the following. nawk '{if (a=length(gsub(/... (2 Replies)
Discussion started by: pinnacle
2 Replies

6. Shell Programming and Scripting

Trim Filename

Hi All, I have a file named as FAB1_600015_CONRAD.A0_7XYZ12345.000_LT-SWET.01_LTPA25L_20110622-161429_07_WFR12345_20110622-161429_20110712-125228.data.dis I want to generate a directory taking only the 7XYZ12345.000_WFR12345 The length and format of the Filename will be the same... (2 Replies)
Discussion started by: asheshrocky
2 Replies

7. Shell Programming and Scripting

trim and compare

1) I want to trim the zeros of the 1st file so they match the second file 2) run an automatic diff and error in lines that dont match File1 0.8035500 1.4138000 1.6381500 1.9256110 3.8075000 13.3270000 13.4155000 94.2700000 937.7000000 File2 0.80355 1.4138 1.63815 3.8075 1.925611... (5 Replies)
Discussion started by: sigh2010
5 Replies

8. Shell Programming and Scripting

another trim question using tr

Hi all, I've been looking for how to eliminate blank spaces in a variable or strings. I've seen several ways, using sed, awk and even python. One of them is use 'tr' command, but it does not work as I expected: For example: echo " stuff " | tr -s " "leaves one space ahead and another... (3 Replies)
Discussion started by: AlbertGM
3 Replies

9. UNIX for Dummies Questions & Answers

trim file

Hi, I have a 6G log , which is unusual to read and I want to minimize it by removing some part on the upper portion( around 4GB). what should i do? can you please help me? thanks. (1 Reply)
Discussion started by: tungaw2004
1 Replies

10. Shell Programming and Scripting

Trim

Hello, I am passing a filename to a script to draw parameters from it. However, I want to use part of the filename as a parameter. The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it. Is this possible? Any help or suggestions would be appreciated! Regards, J. (4 Replies)
Discussion started by: JWilliams
4 Replies
Login or Register to Ask a Question