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 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
input.txt
hello
cruel
world
I want to print all the lines from the above file such that they have a beginning and ending string added along with a delimiter.
BEGIN='
END='
DELIMITER=|
Expected output:
'hello'|'cruel'|'world'
I would GNU AWK for this task following way, let file.txt content be
hello
cruel
world
then
awk '{printf "%s\047%s\047",(NR>1?"|":""),$0}' file.txt
gives output
'hello'|'cruel'|'world'
Explanation: I use printf with 2 places to fill (denoted to %s) and 2 ' (as they have special meaning we must not use just ' but escaped version, that is \047) and so-called ternany operator (condition?valueiftrue:valueiffalse) to use | for lines after first (NR>1) and empty string for all else to fill 1st place and content of whole line ($0) to fill 2nd place.
(tested in gawk 4.2.1)
Related
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 5 months ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Consider following file.
Dalot
# Really empty line
Eriksen
# Line containing space
Malacia
# Line containing tab
# Really empty line
Varane
How do I remove line that ONLY contain either whitespace or tab on it, and leaving empty line intact. The other answer here mostly will remove all empty line including spaces and tab.
Following is desired output.
Dalot
# Really empty line
Eriksen
Malacia
# Really empty line
Varane
Using awk:
awk '/^$/ || NF' file
sed -E '/^[\t ]+$/d'
i.e. "If the line contains spaces and tabs only, remove it".
This might work for you (GNU sed):
sed '/\S/!d' file
Delete all lines that do not contain at least one non-white spaced character.
Alternative:
sed '/^\s*$/d' file
$ awk '/^(|[^\t ]+)$/' file
or
$ sed -En '/^(|[^\t ]+)$/p' file
print if no chars or at least one non-whitespace char.
Note that #choroba did the reverse of this logic to delete lines, which is actually smarter.
cat file | tr ' ' | tr '\t' > f2
mv f2 file
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 have a CSV file that I want to search and find the first occurrence then replace the second occurrence with the first
Here is an example of courses.csv
"Foo","Bar",course foo,Lorem
"Baz","Foo",course bar,Lorem
In the above CSV, I want to replace the second string in the double quotes with the first string in the double quotes
After the operation, the results should be as follow
"Foo","Foo",course foo,Lorem
"Baz","Baz",course bar,Lorem
How can I achieve this preferably on bash?
Assuming you'll always want to put the first field in place of the second field, I'd urge you to try the following:
awk 'BEGIN{FS=",";OFS=","}{$2=$1;print $0}' yourfile.csv
alternatively
awk -v FS="," -v OFS="," '{$2=$1;print $0}' yourfile.csv
this is not in-place: that would require some more effort, but the gist is this.
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 search for this string abcdefgh in a very large file like this and i don't know at which position the new line begin. My first thought was to remove all \n, but the file is over 3 gb ... I think there is smart way to do this (sed, awk, ...?)
efhashivia
hjasjdjasd
oqkfoflABC
DEFGHqpclq
pasdpapsda
Assuming that your search string cannot expand into more than 2 lines, you can use this awk:
awk -v p="ABCDEFGH" 's $0 ~ p {print NR,s $0} {s=$0}' file
Or you can paste each line with its next one, and grep the result. This way you have to create a file with double size of your large input.
tail -n +2 file | paste -d '' file - > output.txt
> cat output.txt
efhashiviahjasjdjasd
hjasjdjasdoqkfoflABC
oqkfoflABCDEFGHqpclq
DEFGHqpclqpasdpapsda
pasdpapsda
> grep -n ABCDEFGH output.txt
3:oqkfoflABCDEFGHqpclq
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 4 years ago.
Improve this question
Remove all lines that don't contain a letter from the alphabet (upper or lower case)
Input :
34
76
0hjjAby68xp
H5e
895
Output :
0hjjAby68xp
H5e
With GNU grep:
grep '[[:alpha:]]' file
or GNU sed:
sed '/[[:alpha:]]/!d' file
Output:
0hjjAby68xp
H5e
Using awk:
$ awk '/[[:alpha:]]/' file
0hjjAby68xp
H5e
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
I have a file with the following format:
AACCCGTAGATCCGAACTTGTG
ACCCGTAGATCCGAACTTGTG
CCGTAGATCCGAACTTGTG
CGTAGATCCGAACTTGT
I want to give a header to each line, using awk, where the header is equal to the line that follows, like this:
>AACCCGTAGATCCGAACTTGTG
AACCCGTAGATCCGAACTTGTG
>ACCCGTAGATCCGAACTTGTG
ACCCGTAGATCCGAACTTGTG
>CCGTAGATCCGAACTTGTG
CCGTAGATCCGAACTTGTG
>CGTAGATCCGAACTTGT
CGTAGATCCGAACTTGT
Simply:
$ awk '{print ">"$0;print}' file
>AACCCGTAGATCCGAACTTGTG
AACCCGTAGATCCGAACTTGTG
>ACCCGTAGATCCGAACTTGTG
ACCCGTAGATCCGAACTTGTG
>CCGTAGATCCGAACTTGTG
CCGTAGATCCGAACTTGTG
>CGTAGATCCGAACTTGT
CGTAGATCCGAACTTGT
Or:
$ awk '{printf ">%s\n%s\n",$0,$0}' file
>AACCCGTAGATCCGAACTTGTG
AACCCGTAGATCCGAACTTGTG
>ACCCGTAGATCCGAACTTGTG
ACCCGTAGATCCGAACTTGTG
>CCGTAGATCCGAACTTGTG
CCGTAGATCCGAACTTGTG
>CGTAGATCCGAACTTGT
CGTAGATCCGAACTTGT
The -v flag allows you to set a variable. Then for each line in the file print that variable followed by the line, and then the line itself.
awk -v c=">" '{ print c $0; print $0; }' <file>