Append a Command to the file in Unix - unix

how do we append a command to the first line of the file?
for example a command to display date?Thank in advance for your answer

It is not possible to prepend directly to a file without overwriting the file.
This may work for you instead.
mv file1.txt file1.txt.tmp # Move file temporarily
date > file1.txt # Add your command
cat file1.txt.tmp >> file1.txt # Append original content
rm file1.txt.tmp # Remove temporary file

Related

Unix Loop through files, grab specific text in the files and create new files with those lines only

I have folder that contains about a 100 files. I want to search for a specific word in each file, pick up only those lines from each file and put them in separate files. For example:
Look for lines that contain the word "HELLO" (case-sensitive) in FILE1.txt, FILE2.txt, FILE3.txt.
From FILE1, copy those lines and put them in a new file, FILE1_BK.txt
From FILE2, copy those lines and put them in a new file, FILE2_BK.txt
From FILE3, copy those lines and put them in a new file, FILE3_BK.txt
I know how to do this one file at a time but am unable to write the script for multiple files at once.
grep "HELLO" FILE1.txt > FILE1_BK.txt
Need to incorporate it in the loop.
for f in myFolder; do something; done;
Thanks in advance!
Why loop through it manually? Why not grep "HELLO" *.txt > FILE1_BK.txt ?
for f in *.txt; do
grep "HELLO" $f > $f_found.txt;
done
Lets say you have filepaths stored in a file named filenames.txt Then you can do:
while read -r filepath; do
grep -r "pattern" $filepath > "$filepath""-output"
done < filenames.txt
This obviously assumes all filepaths are in new lines in filenames.txt
for f in ./myFolder/*; do grep "SEARCHWORD" $f > $f.bak; done;

Adding text to the beginning of a text file without having to copy the entire file in R

I have many large text files, and I would like to add a line at the very beginning. I saw someone had asked this already here. However, this involves reading the entire text file and appending it to the single line. Is there a better (faster) way?
I tested this on windows 7 and it works. Essentially, you use the shell function and do everything on the windows cmd which is quite fast.
write_beginning <- function(text, file){
#write your text to a temp file i.e. temp.txt
write(text, file='temp.txt')
#print temp.txt to a new file
shell(paste('type temp.txt >' , 'new.txt'))
#append your file to new.txt
shell(paste('type', file, '>> new.txt'))
#remove temp.txt - I use capture output to get rid of the
#annoying TRUE printed by file.remove
dump <- capture.output(file.remove('temp.txt'))
#uncomment the last line below to rename new.txt with the name of your file
#and essentially modify your old file
#dump <- capture.output(file.rename('new.txt', file))
}
#assuming your file is test.txt and you want to add 'hello' at the beginning just do:
write_beginning('hello', 'test.txt')
On linux you just need to find the corresponding command in order to send a file to another one (I really think you need to replace type by cat on linux but I cannot test right now).
You'd use the system() function on a Linux distro:
system('cp file.txt temp.txt; echo " " > file.txt; cat temp.txt >> file.txt; rm temp.txt')

Is there any way to extract only one file (or a regular expression) from tar file

I have a tar.gz file.
Because of space issues and the time required extract is longer, I need to extract only the selected file.
I have tried the below
grep -l '<text>' *
file1
file2
only file1,file2 should be extracted.
What should I Do to SAVE all the tail -f data to a FILE swa3?
I have swa1.out which has list of online data inputs.
swa2 is a file which should skip the keywords from swa1.
swa3 is a file where it should write the data.
Can anyone help in this?
I have tried below commnad, but I'm not able to get it
tail -f SWA1.out |grep -vf SWA2 >> swa3
You can use do this with --extract option like this
tar --extract --file=test.tar.gz main.c
Here in --file , specify the .gz filename and at the end specify the
filename you want to extract.

Converting Filename to Filename_Inode

I'm writing my first script that takes a file and moves it to another folder, except that I want to change the filename of the file to filename_inode instead of just filename incase there are any files with the same name
I've figured out how to show this by creating the following 4 variables
inode=$(ls -i $1 | cut -c1-7) #lists the file the user types, cuts the inode from it
space="_" #used to put inbetween the filename and bname
bname=$(basename $1) #gets the basename of the file without the directory etc
bnamespaceinode=$bname$space$inode #combines the 3 values into one variable
echo "$bnamespaceinode #prints filename_inode to the window
So the bottom echo shows filename_inode which is what I want, except now when I try to move this using mv or cp i'm getting the following errors
I dont think it's anything wrong with the syntax i'm using for the mv and cv commands, and so I'm thinking I need to concatenate the 3 variables into a new file or use the result of the first and then append the other 2 to that file?
I've tried both of the above but still not having any luck, any ideas?
Thanks
Without clearer examples, I guess this could work:
$TARGETDIR=/my/target/directory
mv $1 $TARGETDIR/$(basename "$1" | sed 's/_.*/_inode/')

How to append a dynamic string to the end of a copy's filename

Conside File is :
/home/dev/a1234.txt
I want to copy this file in /home/sys/ directory but I want to add some number on the file name while copying.
Date1=date +%Y%m%d_%H:%M:%S
Output should be :
/home/sys/a1234.txt.$Date1
Number on the filename "1234" will be different everytime. so File name is not fixed.
Please suggest.
Something like this should get you on the way:
for i in $( ls /home/dev | grep 'a[0-9]*.txt$'); do
cp /home/dev/$i /home/sys/$i.`date +%Y%m%d_%H:%M:%S`;
done
You can improve it by seeing if the file has already been copied, and prevent it from being copied a second time.
NAME=a1234.txt
cp dev/$NAME sys/$NAME.$Date1
Use the date command to get the current date.
$ DATE=`date +%Y%m%d_%H:%M:%S`; NAME=a; EXT=txt; for i in {0..4}; do echo $NAME$i$.$EXT.$DATE; done
a0.txt.20130625
a1.txt.20130625
a2.txt.20130625
a3.txt.20130625
a4.txt.20130625
Change the echo line to be a cp:
$ DATE=`date +%Y%m%d_%H:%M:%S`; FROMDIR=/home/dev; TODIR=/home/sys; NAME=a; EXT=txt; for i in {0..4}; do cp $FROMDIR/$NAME$i.$EXT $TODIR/$NAME$i$.$EXT.$DATE; done
This is probably better in a bash script where you can modify it easier than a single liner:
#!/bin/bash
# get current date
DATE=`date +%Y%m%d_%H:%M:%S`;
# specify from directory and to directory for cp
FROMDIR=/home/dev;
TODIR=/home/sys;
# set base filename and extension
NAME=a;
EXT=txt;
# count from 0 to 4
for i in {0..4}; do
# copy file and add date to end of new filename
cp $FROMDIR/$NAME$i.$EXT $TODIR/$NAME$i$.$EXT.$DATE;
done
i increments from 0 to 4 in this example, while a filename of the form $NAME$i.$EXT is copied from $FROMDIR to TODIR -- with the current date in $DATE appended to the end of the copy's filename.
In two steps:
Copy the file to /home/sys.
cp /home/dev/a1234.txt /home/sys
Move from a1234.txt to a1234.txt.130625. This way you can make it more general.
mv /home/sys/a1234.txt{,$date} #is expanded to mv /home/sys/a1234.txt /home/sys/a1234.$date
As you indicate your file name will change, you can generalise my answer with $file_name instead of a1234.txt.
Test
$ touch a
$ date=23
$ mv aa{,$date}
$ ls
aa23

Resources