How to copy a file with line number in linux? [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am noob, and not good at linux
I want to copy specific line(20~29) of the file that containing its original line number.
So, I need a new file starting with 20.
Just like this
20 ~~~
21 ~~~
22 ~~~
.
.
.
29 ~~~
(vertically)
I need your help.
Thanks

You can use the following command:
awk 'NR >= 10 && NR <= 20 { print NR,$0}' filename
This will read the file filename and print the line number (NR) followed by the line ($0). The conditions NR >= 10 and NR <= 20 allow you to choose which lines you want to extract.
If you want to save it in a file, you can redirect the output to a file like so:
awk 'NR >= 10 && NR <= 20 { print NR,$0}' filename > new_file
It will put the line numbers and the lines inside a new file called new_file.
awk is a powerfull program which can perform many operations on a text file/stream. I recommand you to learn how to use this tool if you want to achieve complicated things easily in linux.

It can be done in many ways-
Using head- tail command-
head -n 29 your_file.txt |tail -n -10 > new_file.txt
Using sed command- is stream editor and it can perform lot’s of function on file like, searching, find and replace, insertion or deletion.
sed -n '20,29p' your_file.txt > new_file.txt
Using awk command- is used for selecting & manipulating data in a file or passed data passed in runtime
awk 'NR>=20 && NR<=29' your_file.txt > new_file.txt

Related

grep, awk and sed alternatives for python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
os.system(r"grep -R 'Webpage\|Thumbnail' tmp | awk -F ' ' '{print $2}' | sed '1~2s/\(.*\)/]\[img]\1\[\/img]\[\/URL]/g ; N;s/\(.*\)\n\(.*\)/\2\1/ ; s/^/\[URL=/' | tr -d '[:space:]' > ./" + t + ".files/bbcode.txt")
What its doing, grep lines with Keywords in tmp > awk split at space delimiter to get everything after the keywords > sed add "[img]" at start and "[/img][/URL]" at the end of every line > sed add "[URL=" to start and "]" at the end of every second line > move all odd lines to beginning of all even lines > remove all spaces and merge into one big line.
Please can someone point me in the right direction to do this in python?
Here is a simple stab at a Python replacement.
grep -R will recursively search regular files in the destination directory. This can be replaced with os.walk('tmp'). Remember that the third result from os.walk is just the file names; you have to glue back the directory in front of each one.
Fields are generally numbered starting with 1 in the Unix command-line tools, while Python's indexing is zero-based. So the second field from the line is line.split(' ')[1], not line.split(' ')[2]
Without access to your files, I had to guess what the sed script is really receiving as input. I'm assuming that every second output is a "Webpage" one and every other is a "Thumbnail" one.
Tangentially, piping Awk to sed and tr is basically useless; Awk can do everything those two tools can do all by itself (though a nontrivial sed script might be hard to reimplement in Awk -- but this is not an example of that. 1~2 is a GNU sed extension so this was never very portable to begin with, and would be a lot easier to read and understand in Awk.). Conversely, splitting on a single space with Awk is kind of overkill; cut -d ' ' -f2 would be a more economical and succinct way to do that.
import os
with open(t + ".files/bbcode.txt", "w") as bbcode:
for root, dirs, files in os.walk('tmp'):
for file in files:
with open(os.path.join(root, file)) as lines:
idx = 0
for line in lines:
if 'Webpage' in line or 'Thumbnail' in line:
idx += 1
field = line.split(' ')[1]
if idx % 2 == 1:
thumb = field
next
bbcode.write(
'[URL=%s][img]%s[/img][/URL]' % (field, thumb))
The decision to collect all output on a single long line is dubious; could you perhaps be persuaded to add a final \n to the write format string?

Find number 9 in file - linux terminal [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to find how many times number "9" appears in the result of the draw
(ignoring the ordinal number and date)
65. 11.05.1958 8,17,22,27,31,21
66. 18.05.1958 1,2,8,17,28,54
67. 25.05.1958 7,16,27,33,41,23
68. 01.06.1958 1,20,41,42,43,43
69. 08.06.1952 13,14,25,29,33,47
70. 15.06.1958 17,23,29,39,41,45
71. 22.06.1958 2,14,22,44,48,49
72. 29.06.1958 3,7,13,15,16,47
73. 06.07.1958 10,11,28,38,48,49
74. 13.07.1956 9,16,21,24,27,35
75. 20.07.1958 1,7,17,18,29,32
76. 27.07.1958 19,21,42,25,36,44
77. 03.08.1958 2,4,22,31,32,43
78. 10.08.1958 4,9,16,26,27,46
79. 17.08.1958 34,35,37,38,39,45
80. 24.08.1958 17,21,27,35,41,49
81. 31.08.1958 30,31,32,9,46,49
82. 07.09.1958 10,16,23,26,30,39
83. 14.09.1958 13,16,18,19,30,35
84. 21.09.1958 9,23,26,29,31,42
85. 28.09.1958 12,16,21,28,9,49
Use awk to remove the first 2 columns, then use grep -c to find the 9s. Put a \b around the regex to make sure it doesn't track 19,29,91,92,93,etc. Assuming your output is coming from FILENAME:
awk '{ print $3 }' FILENAME | grep -c '\b9\b
Assuming the text to search is in a file named output.txt:
cut <output.txt -d ' ' -f 3 | grep -w 9 | wc -l
The cut part splits by spaces, taking the third field.
grep -w finds 9 as a word, so the for example the line with 10,16,23,26,30,39 won't get picked.
Finally, wc -l counts how many lines we have.
And just for fun, here is yet another way, this one in Perl:
perl -nle '$c+=()=/\b(9)\b/g; END {print $c}' $FILENAME
This has the difference that it allows 9 to appear multiple times, and counts them all. Because it uses /g (global) and first casts the result to an array ()=, before adding the number of elements in the array to $c.

How to create a Unix script to segregate data Line by Line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have some data in a MyFile.CSV file like this:
id,name,country
100,tom cruise,USA
101,Johnny depp,USA
102,John,India
What will be the shell script to take the above file as input and segregate the data in 2 different files as per the country?
I tried using the FOR loop and then using 2 IFs inside it but I am unable to do so. How to do it using awk?
For LINE in MyFile.CSV
Do
If grep "USA" $LINE >0 Then
$LINE >> Out_USA.csv
Else
$LINE >> Out_India.csv
Done
You can try with this
grep -R "USA" /path/to/file >> Out_USA.csv
grep -R "India" /path/to/file >> Out_India.csv
Many ways to do:
One way:
$ for i in `awk -F"," '{if(NR>1)print $3}' MyFile.csv|uniq|sort`;
do
echo $i;
egrep "${i}|country" MyFile.csv > Out_${i}.csv;
done
This assumes that the country name would not clash with other columns.
If it does, then you can fine tune that by adding additional regex.
For example, it country will be the last field, then you can add $ to the grep

Change the path address in a text file by shell scripting [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my Bash script, I have to change a name to a path address(new address) in a text file:
(MYADDREES) change to ( /home/run1/c1 ) and save it as new file.
I did like this: defined a new variable = new address and tried to replace it in previous address in text file.
I use sed but it has problem.
My script was:
#!/bin/bash
# To debug
set -x
x=`pwd`
echo $x
sed "s/MYADDRESS/$x/g" < sample1.txt > new.txt
exit
The output of pwd is likely to contain / characters, making your sed expression look something like s/MYADDRESS//home/user/somewhere/. This makes it impossible for sed to sort out what should be replaced with what. There are two solutions:
Use a different delimiter for sed:
sed "s,MYADDRESS,$x,g" < sample1.txt > new.txt
...although this will have the same problem if the current path contains a comma character or something else that is a special character for sed, so the more robust approach is to use awk instead:
awk -v curdir="$(pwd)" '{ gsub("MYADDRESS", curdir); print }' < sample1.txt > new.txt

How to use Linux to read a file line by line and replace all the spaces into ','? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am a beginner.. I'd like to use Linux shell to make the following file
1 2 2
2 3 4
4 5 2
4 2 1
....
into
1,2,2
2,3,4
4,5,2
4,2,1
Thank you very much!
Are you looking for something like this:-
sed -e "s/ /,/g" < a.txt
or may be easier like this:
tr ' ' ',' <input >output
or in Vim you can use the Regex:
s/ /,/g
The question asks "line by line". In bash :
while read line; do echo $line | sed 's/ /,/g'; done < file
It will read file line by line into line, print (echo) each line and pipe (|) it to sed which will change spaces into commas. You can add > newfile at the end (but > file won't work) if you need to store it in a file.
But if you don't need anything else than changing characters in the file, processing the whole file at once is easier and probably quicker :
sed -i 's/ /,/g' file
(option -i is for modifying the file directly, as opposed to print modifications to stdout).
Read more about sed to understand its syntax, you'll need it eventually.

Resources