Deleting first row of each csv file using sed - bash

I am trying to delete the first row of each of my .csv files in my DIR folder using the sed:
DIR=/home/results
for filename in "$DIR"; do
sed 1d filename.csv
done
However, this doesn't work. I am new to bash scripting and would be thankful if anyone tells me how to fix this.

Just do:
for f in /home/results/*.csv; do sed -i '1 d' "$f"; done
The glob pattern /home/results/*.csv matches all .csv files in /home/results/ directory and then the for construct iterate over the files, sed does the in place removal of the first row from each file.

Related

Find and Replace word in text files based on name of text file

I have 2 files: A.txt, B.txt
Both these files contain the letter NODE in them.
I want to replace NODE in each of the files with A_NODE and B_NODE in A.txt and B.txt, respectively.
I know there is a for loop and sed involved, but not able to figure this one out. Any suggestions?
I was thinking:
for file in *; do sed -i 's/NODE/$file.NODE/g' file; done
for file in *; do sed -i "s/NODE/$(basename -s .txt file)_NODE/g" file; done
The double quotes (") instead of single quotes (') enable bash variable interpolation (replacing the $ expression).
$(...) executes an arbitrary shell command and inserts the result.
basename -s .txt removes the directory part and the suffix .txt from the filename.
Thank you #sapanoia:
This works:
for file in *; do sed -i "s/NODE/$(basename -s .txt "$file")_NODE/g" $file; done

Iterating through files in a folder with sed

I've a list of csv-files and would like to use a for loop to edit the content for each file. I'd like to do that with sed. I have this sed commands which works fine when testing it on one file:
sed 's/[ "-]//g'
So now I want to execute this command for each file in a folder. I've tried this but so far no luck:
for i in *.csv; do sed 's/[ "-]//g' > $i.csv; done
I would like that he would overwrite each file with the edit performed by sed. The sed commands removes all spaces, the " and the '-' character.
Small changes,
for i in *.csv
do
sed -i 's/[ "-]//g' "$i"
done
Changes
when you iterate through the for you get the filenames in $i as example one.csv, two.csv etc. You can directly use these as input to the sed command.
-i Is for inline changes, the sed will do the substitution and updates the file for you. No output redirection is required.
In the code you wrote, I guess you missed any inputs to the sed command
In my case i want to replace every first occurrence of a particular string in each line for several text files, i've use the following:
//want to replace 16 with 1 in each files only for the first occurance
sed -i 's/16/1/' *.txt
In your case, In terminal you can try this
sed 's/[ "-]//g' *.csv
In certain scenarios it might be worth considering finding the files and executing a command on them like explained in this answer (as stated there, make sure echo $PATH doesn't contain .)
find /path/to/csv/ -type f '*.csv' -execdir sed -i 's/[ "-]//g' {} \;
here we:
find all files (type f) which end with .csv in the folder /path/to/csv/
sed the found files in place, ie we replace the original files with the changed version instead of creating numbered csv files ($i.csv)

how to remove <Technology> and </Technology> words from a file using shell script?

My text file contains 100 lines and the text file surely contains Technology and /Technology words .In which ,I want to remove Technology and /Technology words present in the file using shell scripting.
sed -i.bak -e 's#/Technology##g' -e 's#Technology##g' my_text_file
This is delete the words and also make a backup of the original file just in case
sed -i -e 's#/Technology##g' -e 's#Technology##g' my_text_file
This will not make a backup but just modify the original file
You can try this one.
sed -r 's/<[\/]*technology>//g' a
Here is an awk
cat file
This Technology
More here
Mine /Technology must go
awk '{gsub(/\/*Technology/,"")}1' file
This
More here
Mine must go
By adding an extra space in the regex, it will not leave an extra space in the output.
awk '{gsub(/\/*Technology /,"")}1' file
This Technology
More here
Mine must go
To write back to original file
awk '{gsub(/\/*Technology /,"")}1' file > tmp && mv tmp file
If you have gnu awk 4.1+ you can do
awk -i '{gsub(/\/*Technology /,"")}1' file

How to append a character from a filename to the end of each line of a file from a terminal?

I know how to do this using Python, but I'd like to learn how to do it with awk or sed or whatever the best command line method. I have file names that look like
A1.txt
A2.txt
...
B99.txt
B100.txt
Each file contains one line. I'd like to append the letter in the file name to the end of the line. Then the contents of the files would look like:
a one file contents A
a two file contents A
...
b ninety nine file contents B
b one hundred file contents B
If you want to change the files in place, sed's -i option is handy:
for f in *.txt; do ltr=${f:0:1}; sed -i "s/$/ $ltr/" "$f" ; done
Explanation:
for f in *.txt
This starts a loop over all .txt files in the current directory.
ltr=${f:0:1}
This extracts the first character from the file name
sed -i "s/$/ $ltr/" "$f"
This replaces the end of each line in the file named by f with a space and the first character of the file name. In more detail:
-i tells sed to make the changes in place.
s/$/ $ltr/ is the substitute command. The format is s/old/new/ where, here, old is $ which matches at the end of the line, and new is $ltr which is a space and the first character of the file's name.
"$f" tells sed top operate of the file named by f.
How about pure bash?
for file in *.txt; do
IFS= read -r line < "$file"
printf '%s %s\n' "$line" "${file::1}" > "$file"
done

bash removing part of a file name

I have the following files in the following format:
$ ls CombinedReports_LLL-*'('*.csv
CombinedReports_LLL-20140211144020(Untitled_1).csv
CombinedReports_LLL-20140211144020(Untitled_11).csv
CombinedReports_LLL-20140211144020(Untitled_110).csv
CombinedReports_LLL-20140211144020(Untitled_111).csv
CombinedReports_LLL-20140211144020(Untitled_12).csv
CombinedReports_LLL-20140211144020(Untitled_13).csv
CombinedReports_LLL-20140211144020(Untitled_14).csv
CombinedReports_LLL-20140211144020(Untitled_15).csv
CombinedReports_LLL-20140211144020(Untitled_16).csv
CombinedReports_LLL-20140211144020(Untitled_17).csv
CombinedReports_LLL-20140211144020(Untitled_18).csv
CombinedReports_LLL-20140211144020(Untitled_19).csv
I would like this part removed:
20140211144020 (this is the timestamp the reports were run so this will vary)
and end up with something like:
CombinedReports_LLL-(Untitled_1).csv
CombinedReports_LLL-(Untitled_11).csv
CombinedReports_LLL-(Untitled_110).csv
CombinedReports_LLL-(Untitled_111).csv
CombinedReports_LLL-(Untitled_12).csv
CombinedReports_LLL-(Untitled_13).csv
CombinedReports_LLL-(Untitled_14).csv
CombinedReports_LLL-(Untitled_15).csv
CombinedReports_LLL-(Untitled_16).csv
CombinedReports_LLL-(Untitled_17).csv
CombinedReports_LLL-(Untitled_18).csv
CombinedReports_LLL-(Untitled_19).csv
I was thinking simply along the lines of the mv command, maybe something like this:
$ ls CombinedReports_LLL-*'('*.csv
but maybe a sed command or other would be better
rename is part of the perl package. It renames files according to perl-style regular expressions. To remove the dates from your file names:
rename 's/[0-9]{14}//' CombinedReports_LLL-*.csv
If rename is not available, sed+shell can be used:
for fname in Combined*.csv ; do mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" ; done
The above loops over each of your files. For each file, it performs a mv command: mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" where, in this case, sed is able to use the same regular expression as the rename command above. s/[0-9]{14}// tells sed to look for 14 digits in a row and replace them with an empty string.
Without using an other tools like rename or sed and sticking strictly to bash alone:
for f in CombinedReports_LLL-*.csv
do
newName=${f/LLL-*\(/LLL-(}
mv -i "$f" "$newName"
done
for f in CombinedReports_LLL-* ; do
b=${f:0:20}${f:34:500}
mv "$f" "$b"
done
You can try line by line on shell:
f="CombinedReports_LLL-20140211144020(Untitled_11).csv"
b=${f:0:20}${f:34:500}
echo $b
You can use the rename utility for this. It uses syntax much like sed to change filenames. The following example (from the rename man-page) shows how to remove the trailing '.bak' extension from a list of backup files in the local directory:
rename 's/\.bak$//' *.bak
I'm using the advice given in the top response and have put the following line into a shell script:
ls *.nii | xargs rename 's/[f_]{2}//' f_0*.nii
In terminal, this line works perfectly, but in my script it will not execute and reads * as a literal part of the file name.

Resources