How to remove first 2 lines of a file in a script
Hi All,
I have a file that is ftp'd each night. The file needs to have the first 2 lines deleted before being sent via ftp. How do I put that into the script? It needs to open the file,remove first two lines and then save the file with the lines removed. Thanks, Nazs |
Code:
#!/bin/csh hope this helps. |
Code:
sed -n -i '3,$ p' filename |
..or that!
|
Code:
awk '{ if( NR > 2 ) { print } }' currentfile > newfile |
Thank you all for the quick responses. I still have some questions. Each file that is created has a counter. The file will always start with 2351 and then has a counter after that. The script looks for 2351*.OUT . So it might be 2351AA.OUT. The next one would be 2351AB.OUT and so on. For now i was trying the sed -n -i '3,$ p' filename which i put in bold and red. So i am not sure what to put down for the file name after sed -n -i '3,$ p' filename. Here is my script below.
#!/bin/sh UNIDUMP=/usr/pa/universe/unidump/ ARCHIVE=/usr/pa/universe/unidump/bin_uni/archive/ LOG=/var/tmp/unisend.log cd $UNIDUMP #CHECK IF THERE ARE ANY FILES gunzip $UNIDUMP/2351*.OUT.gz if [ `find . -type f -maxdepth 1 -name "2351*.OUT*" | wc -l` -eq 0 ]; then echo "Exit NO FILES" exit else COUNTER=`find . -type f -maxdepth 1 -name "2351*.OUT"` for i in $COUNTER;do sed -n -i '3,$ p' filename echo "record removed" su slp -c "/usr/bin/gpg --no-tty -vsear AOB --passphrase-fd 0 $i< ~lsp/.gnupg/pw" # FTP FILE TO AOB Thanks again, Nazs |
Please post code in [code] tags to improve readability.
No need to assign the list of files to a variable and then use it. Use xargs: Code:
... |
I am getting this error:
sed: invalid option -- i Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]... -n, --quiet, --silent suppress automatic printing of pattern space -e script, --expression=script add the script to the commands to be executed -f script-file, --file=script-file add the contents of script-file to the commands to be executed --help display this help and exit -V, --version output version information and exit If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read. E-mail bug reports to: bug-gnu-utils@gnu.org . Be sure to include the word ``sed'' somewhere in the ``Subject:'' field. Nazs |
What do you get from this command:
Code:
sed --version |
the sed version OP is using should probably be
Code:
GNU -- 3.02 |
Wow, that's quite old. The version I have is 4.1.5. You can use a temporary file instead of the -i option (which says to edit a file in-place):
Code:
find . -type f -maxdepth 1 -name "2351*.OUT" | while read file; do |
Hi.
Here is an alternative to sed for skipping the first 2 lines of a file, namely by using tail: Code:
#!/bin/sh Code:
% ./s1 ( edit 1: typo ) |
You could also do it with ed. This command will work so long as your echo command supports the -e option (expand \ escaped characters). This is another nice one in that it doesn't create a second file - it modifies in-place:
Code:
echo -e "1,2 d\nw" | ed file Code:
(echo "1,2 d" ; echo "wq")| ed file |
I just want to thank everyone for helping me. Mathewg42 this is what worked for me:
echo -e "1,2 d\nw" | ed file Thanks alot! Nazs |
Quote:
Code:
for i in 2351*.OUT |
All times are GMT -5. The time now is 03:59 PM. |