Hello I would like to add the word: tarvell: before each word on every line.
The file is 1Gb so I would like to be able to do it through my VPS if possible.
Im guessing some sort of grep/awk program could do it. Thanks.
Current:
Line1
Line2
Line3
Want:
tarvellLine1
tarvellLine2
tarvellLine3
Q: Im guessing some sort of grep/awk program could do it
A: Yes, indeedy.
I'm guessing your VPS is probably running Linux (or a close *nix variant), and that you have a command prompt (e.g. a putty terminal).
Try "sed".
EXAMPLE:
# Create input file
vi tmp.txt
abc
def
geh
# Add "tarvell" to the beginning of each line; write to a second file
sed 's/^/tarvell/' tmp.txt > tmp2.txt
# Print the results
cat tmp2.txt
tarvellabc
tarvelldef
tarvellgeh
sed 's/\(^\|\W\)\(\w\)/\1tarvell\2/g' -i filename
original
one two
three (four), five
updated
tarvellone tarvelltwo
tarvellthree (tarvellfour), tarvellfive
Related
I have a command that will print out three lines:
1-foo-1
1-bar-1
1-baz-1
I would like to include the result of this as part of a command where I search for the line that contains the string "bar" and then include that entire line as part of a command as follows:
vi 1-bar-1
I was wondering what the bash awk and/or grep combination would be for getting this. Thank you.
I had tried the following but I'm getting the entire output. For example, I'd have a file rows.txt with this content:
1-foo-1
1-bar-1
1-baz-1
and then I'd run echo $(cat rows.txt | awk /^1-baz.*$/) and I'd get 1-foo-1 1-bar-1 1-baz-1 as a result when I'm looking for just 1-baz-1. Thank you.
vi $(echo -e "1-foo-1\n1-bar-1\n1-baz-1\n" | grep bar | awk -F'-' '{print $2}')
The above script would equals vi bar
P.S.
echo -e "1-foo-1\n1-bar-1\n1-baz-1\n" is a demo to mimic your command output.
P.S.
You update the question... Now your goal becomes:
I'm looking for just 1-baz-1.
Then, the solution would be just
cat rows.txt | grep baz
I search for the line that contains the string "bar":
A naive approach would be to just use
vi $(grep -F bar rows.txt)
However, you have to keep in mind a few things:
If your file contains several lines with bar, say
1-bar-1
2-bar-2
the editor will open both files. This may or may not what you want.
Another point to consider: If your file contains a line
1-foobar-1
this would be choosed as well. If you don't want this to happen, use
vi $(grep -Fw bar rows.txt)
The -w option requires that the pattern must be delimited by word boundaries.
I have a problem: I have a file that, if I knew how, I would like to edit from the command. I would like to locate the file by content on that line.
I am in CyberPatriot, and my team is second in my state. I know someone who is on the number one team and I know one of the people on the first team. It kills me so I want to make a list of commands that I can go off of to make it faster and more efficient.
Imagine I had this file:
example
oof
goo
random
yes
and I wanted to change it to this:
example
oof
goo
random 'added text'
yes
How do I do so?
I know I can use the echo command to add text to the end of a file, but I don't know how to add text to the end of a specific line.
Thanks, Owen
You can use sed for this purpose.
sed 's/random/& Hello World/' file
to append text to the matched string.
You can use ^random$ to make sure the entire line is matched, before appending.
If you need to modify the file directly, you can use the -i flag, which facilitates in-place editing. Further, using -i.bak creates a backup of the original file first before modifying it, as in
sed -i.bak 's/random/& Hello World/' file
The original copy of the file can be found in file.bak
More about sed : https://www.gnu.org/software/sed/manual/sed.html
Use something like below
sed '4!d' file | xargs -I{} sed -i "4s/{}/{} \'added text\'/" file
Basically in the above command, we are getting the 4th line of the file using sed sed '4!d' file and then using this line to replace it with the same text and some new text(added text)
I am on macOS High Sierra, and I've had a few issues with sed,
after spending a day on Google and this site, I've honestly tried everything I could think of and was suggested.
I have an example.txt file with 3 lines.
line1
line2
line3
The 3 lines can be anything, I do not know them upfront. (scenario)
And at some point I do know what the line is going to be.
All I wish to achieve is to use 'whatever' onliner that basically says:
in that file, replace line 2, with the new data.
sed -i "2s/.*/$NEW_DATA/" "$FILENAME"
This should work, but on macOS, this does not.
sed -ie "2s/.*/$NEW_DATA/" "$FILENAME"
Note the e? Someone on here suggested to add an e, and yes, that works.. but it means it adds the e behind the .txt. I end up with 2 files, example.txt and example.txte
sed -i'' "2s/.*/$NEW_DATA/" "$FILENAME"
Note the '' behind -i, and yes, without a space? This should work too.
It doesn't, it will complain that command c requires \
I could go on, but the request is quite simple:
on macOS, in a bash shell script, how do I simply replace a specified line of text in a .txt file, with a completely new line of text -- not knowing what the line of text was before?
If this can be a simple macOS one liner with awk or whatever, that's fine. It doesn't have to be sed. But when I search this site, it seems to be the recommended one to go with in this regards.
From the sed man page in macOS:
-i extension
Edit files in-place, saving backups with the specified extension.
If a zero-length extension is given, no backup will be saved.
Therefore this can be used to replace line 2 without keeping backup:
sed -i '' "2s/.*/$NEW_DATA/" testfile.txt
If the goal is just to replace contents of line 2 awk could also be used, for example:
awk 'NR==2{$0="your content"}1' testfile.txt > testfile.tmp && mv testfile.tmp testfile.txt
I have a simple script to read the content of a input file line by line with some extra strings added. Here is one example.
input file: input.txt
content of this input.txt
aaaaaa
bbbbbb
cccccc
dddddd
eeeeee
code to read the file
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "abc.def.hig.ewe.adg.hea.L_${line}.great"
done < "$1"
I'm not sure where exactly it is wrong; I cannot get correct output. It looks like when you add .great at the end of a variable, the output will mess up the sequence.
Transferring comments into an answer.
What output do you get? What output do you expect? I got five lines similar to:
abc.def.hig.ewe.adg.hea.L_aaaaaa.great
using Bash 3.2.57 and Bash 4.3 (on Mac OS X 10.11.5).
Is the data file from Windows or some other source that uses CRLF line endings? That will make things look like:
.greatf.hig.ewe.adg.hea.L_aaaaaa
instead of what I showed before. And, I note, this would have been readily explicable (or discountable) if you'd only showed the output you were getting.
Yes, you are right. The problem is the input file. It got corrupted when uploading from Windows to Unix.
Have you considered using something like awk? It is very good for these types of simple tasks.
$ cat input.txt
aaaa
bbbb
cccc
-
$ awk '{print "prefix_"$0"_suffix"}' input.txt
prefix_aaaa_suffix
prefix_bbbb_suffix
prefix_cccc_suffix
I have a huge csv file (on order of terabytes).
Now, I want to insert one row which is a header to the the top.
For example if input.csv looks like this:
1,2,3,4
22,3,23,1
I want it to look like
id1,id2,id3,id4
1,2,3,4
and so on
How do i do this from shell, terminal, awk, bash?/
In place, using sed:
sed -i 1i"id1,id2,id3,id4" file.csv
edit:
As #Ed Morton points out, using sed with the -i switch sed edits the file in place, and can therefore be dangerous when editing large files. If you supply a prefix after the -i option then sed creates a backup. So something like this would be safer:
sed -i.bak 1i"id1,id2,id3,id4" file.csv
The original file will then be located in file.csv.bak
This is that simple as :
{ echo "id1,id2,id3,id4"; cat file.csv; } > newfile.csv
using simple shell concatenation.
EDIT
after discussion thread below, I propose this :
create a file with your header, said head.txt
Then :
cat head.txt file.csv > newfile.csv
Edit. When I wrote this answer, I overlooked the "terabyte" part of the question. Hence, do not use the method presented here. I still leave this post, as it advertises the use of this wonderful tool, ed, the standard text editor.
As usual, ed is the standard text editor. The solution using sed -i doesn't, as it mentions, "edit the file in place". Instead, it outputs its content to a temporary file, and then renames this file to the original one. That's really not good for large files!
Using ed instead really edits the file. Something along the following lines:
#!/bin/bash
file="input.csv"
{
ed -s "$file" <<EOF
1
i
id1,id2,id3,id4
.
wq
EOF
} > /dev/null
Explanation: 1 goes to the first line, i goes into insert mode, then we insert id1,id2,id3,id4 then . to go back to normal mode, and wq to write and quit.
With this method, you're really editing the file and it's twice faster than the sed method. Also, ed is known to be "large file safe"!
Done.
There's no easy way, you're going to have to rewrite the file. Probably the safest way is to
( echo "id1,id2,id3,id4" ; cat file ) > newFile && rm file
IHTH
echo "id1,id2,id3,id4" >> data.csv