I have a file that has around million lines. I need to go to line number 320123 to check the data. How do I do that?
With n being the line number:
ng: Jump to line number n. Default is the start of the file.
nG: Jump to line number n. Default is the end of the file.
So to go to line number 320123, you would type 320123g.
Copy-pasted straight from Wikipedia.
To open at a specific line straight from the command line, use:
less +320123 filename
If you want to see the line numbers too:
less +320123 -N filename
You can also choose to display a specific line of the file at a specific line of the terminal, for when you need a few lines of context. For example, this will open the file with line 320123 on the 10th line of the terminal:
less +320123 -j 10 filename
You can use sed for this too -
sed -n '320123'p filename
This will print line number 320123.
If you want a range then you can do -
sed -n '320123,320150'p filename
If you want from a particular line to the very end then -
sed -n '320123,$'p filename
From within less (in Linux):
g and the line number to go forward
G and the line number to go backwards
Used alone, g and G will take you to the first and last line in a file respectively; used with a number they are both equivalent.
An example; you want to go to line 320123 of a file,
press 'g' and after the colon type in the number 320123
Additionally you can type '-N' inside less to activate / deactivate the line numbers. You can as a matter of fact pass any command line switches from inside the program, such as -j or -N.
NOTE: You can provide the line number in the command line to start less (less +number -N) which will be much faster than doing it from inside the program:
less +12345 -N /var/log/hugelogfile
This will open a file displaying the line numbers and starting at line 12345
Source: man 1 less and built-in help in less (less 418)
For editing this is possible in nano via +n from command line, e.g.,
nano +16 file.txt
To open file.txt to line 16.
Related
I want to add a string to the file at the end of each line starting from the second line .
so this is my file budget.txt
id,budget
d4385ff7-247f-407a-97c6-366d8128c6c7,
50548d0a-257c-44f5-b175-2e7efa53dc35,
e15965cf-ffc1-40ae-94c4-b450ab190233,
b9286b97-2575-4c98-bd24-1393d5309e76,
the output i am expecting is below. I want to add the string 'True' starting from the second line onwards in the end.
id,budget
d4385ff7-247f-407a-97c6-366d8128c6c7,True
50548d0a-257c-44f5-b175-2e7efa53dc35,True
e15965cf-ffc1-40ae-94c4-b450ab190233,True
b9286b97-2575-4c98-bd24-1393d5309e76,True
what could be the shortest bash command .
thank you so much
appreciate any help
Make sure to run dos2unix budget.txt on your file before running the commands below, in general .txt files are originated on windows so have different line ending.
awk 'NR>1{$0=$0"True"}1' file
id,budget
d4385ff7-247f-407a-97c6-366d8128c6c7,True
50548d0a-257c-44f5-b175-2e7efa53dc35,True
e15965cf-ffc1-40ae-94c4-b450ab190233,True
b9286b97-2575-4c98-bd24-1393d5309e76,True
Here, NR is the number of record and by the default nature of awk record is same as line. So if you do NR>1 it will tell awk to perform action inside {..} on the lines number greater than 1.
Or use sed, here replace end of line $ with True:
sed '2,$s/$/True/' file
id,budget
d4385ff7-247f-407a-97c6-366d8128c6c7,True
50548d0a-257c-44f5-b175-2e7efa53dc35,True
e15965cf-ffc1-40ae-94c4-b450ab190233,True
b9286b97-2575-4c98-bd24-1393d5309e76,True
I need to replace a particular line with a new-text-string ,tried few things but not succeeded.
This is what I did:
1- Get the line number which needs to be replaced using
lineNum=$(sed -n '/Old-Line-Text/=' FileName.txt);
2- Use the Line number from step 1 to replace that line using
sed 'Ns/.*/New-Line-Text/' FileName.txt > FileName2.txt;
Now I need to combine these two commands in one commands. Tried certain things But was't able to get it Fixed.
Better do this :
sed '/Old-Line-Text/s/.*/New-Line-Text/' file
but this can be resumed as just :
sed 's/.*Old-Line-Text.*/New-Line-Text/' file
I have a big file like this small example:
>ENSG00000002587|ENST00000002596
ATGGCCGCGCTGCTCCTGGGCGCGGTGCTGCTGGTGGCCCAGCCCCAGCTAGTGCCTTCC
>ENSG00000004059|ENST00000000233
ATGGGCCTCACCGTGTCCGCGCTCTTTTCGCGGATCTTCGGGAAGAAGCAGATGCGGATT
>ENSG00000003249|ENST00000002501
ATGGAGCCCCCGGAGGGCGCCGGCACCGGAGAGATCGTTAAGGAGGCTGAGGTGCCGCAG
GCTGCGCTGGGCGTCCCAGCCCAGGGGACAGGGGACAATGGCCACACGCCTGTGGAGGAG
>ENSG00000048028|ENST00000003302
ATGACTGCGGAGCTGCAGCAGGACGACGCGGCCGGCGCGGCAGACGGCCACGGCTCGAGC
TGCCAAATGCTGTTAAATCAACTGAGAGAAATCACAGGCATTCAGGACCCTTCCTTTCTC
CATGAAGCTCTGAAGGCCAGTAATGGTGACATTACTCAGGCAGTCAGCCTTCTCACTGAT
I want to remove the first 5 character of every line which is below the line that starts with >.
I do not know how to do that in command line. Do you know?
Here is the expected output:
>ENSG00000002587|ENST00000002596
CGCGCTGCTCCTGGGCGCGGTGCTGCTGGTGGCCCAGCCCCAGCTAGTGCCTTCC
>ENSG00000004059|ENST00000000233
CCTCACCGTGTCCGCGCTCTTTTCGCGGATCTTCGGGAAGAAGCAGATGCGGATT
>ENSG00000003249|ENST00000002501
GCCCCCGGAGGGCGCCGGCACCGGAGAGATCGTTAAGGAGGCTGAGGTGCCGCAG
GCTGCGCTGGGCGTCCCAGCCCAGGGGACAGGGGACAATGGCCACACGCCTGTGGAGGAG
>ENSG00000048028|ENST00000003302
TGCGGAGCTGCAGCAGGACGACGCGGCCGGCGCGGCAGACGGCCACGGCTCGAGC
TGCCAAATGCTGTTAAATCAACTGAGAGAAATCACAGGCATTCAGGACCCTTCCTTTCTC
CATGAAGCTCTGAAGGCCAGTAATGGTGACATTACTCAGGCAGTCAGCCTTCTCACTGAT
sed -E '/^>/{N;s/\n.{5}/\n/}' file
find line starting with >
join that line with next
replace newline and five chars with just newline
I have a file in csv format. I know positions where I want to chip off a chunk from the file and write it as a new csv file.
split command splits a file into equal-sized chunks. I wonder if there exists an effective (the file is huge) way to split file into chunks of different sizes?
I assume you want to split the file at a newline character. If this is the case you can use the head and tail commands to grab a number of lines from the beginning and from the end of your file, respectively.
If you want to copy a new of lines from within the file you can use sed, e.g.
sed -e 1,Nd -e Mq file
where N should be replaced with the line number of the line preceding the first line to display and M should be the line number of the last line to display.
Given that two lines have been printed out in the terminal, is it possible to delete both of them so they may be replaced with two new lines?
I know you can use \r to replace 1 line (well, to move the cursor to the start of the line), but is there any way of doing this for the line above?
As an example, I'm running a program for computing the eigenfunctions of the Schrodinger equation and I want to keep an eye on how my variables are changing as it's being run, so I'd like an output like:
Param 1: xxxxxxx
Param 2: xxxxxxx
So I'd have the two parameters on two lines so they can be easily read and they'd be updated on each iteration of the program's matching function.
The cuu1 terminal capability allows you to go up a line. Pass it to tput in order to read the character sequence from the terminfo/termcap database, and then echo it twice.
echo -e '123\nabc\n'"$(tput cuu1)$(tput cuu1)"'*\n*'
You could also use $(tput cuu 2) instead of $(tput cuu1)$(tput cuu1)
-- Aesthir