Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop for deleting first line of Multiple Files using Bash Script

Tags:

bash

Just new to Bash scripting and programming in general. I would like to automate the deletion of the first line of multiple .data files in a directory. My script is as follows:

#!/bin/bash
for f in *.data ;
do tail -n +2 $f | echo "processing $f";
done

I get the echo message but when I cat the file nothing has changed. Any ideas?

Thanks in advance

like image 767
Marko Avatar asked Nov 17 '13 12:11

Marko


2 Answers

I get the echo message but when I cat the file nothing has changed.

Because simply tailing wouldn't change the file.

You could use sed to modify the files in-place with the first line excluded. Saying

sed -i '1d' *.data

would delete the first line from all .data files.


EDIT: BSD sed (on OSX) would expect an argument to -i, so you can either specify an extension to backup older files, or to edit the files in-place, say:

sed -i '' '1d' *.data
like image 185
devnull Avatar answered Sep 18 '22 15:09

devnull


You are not changing the file itself. By using tail you simply read the file and print parts of it to stdout (the terminal), you have to redirect that output to a temporary file and then overwrite the original file with the temporary one.

#!/usr/bin/env bash
for f in *.data; do
    tail -n +2 "$f" > "${f}".tmp && mv "${f}".tmp "$f"
    echo "Processing $f"
done

Moreover it's not clear what you'd like to achieve with the echo command. Why do you use a pipe (|) there?

sed will give you an easier way to achieve this. See devnull's answer.

like image 40
pfnuesel Avatar answered Sep 20 '22 15:09

pfnuesel