sed

how to extract text which matches particular fields in text file using linux commands

旧时模样 提交于 2021-01-27 11:57:36
问题 Hi below is my text file {"Author":"john" "subject":"java" "title":"java cook book.pdf"} {"title":"Php book.pdf" "Author":"Smith" "subject":"PHP"} {"Author":"Smith" "title":"Java book.pdf"} from the above data i want to extract all titles which contains "java" word, i should get the following output java cook book.pdf Java book.pdf Please suggest me Thanks 回答1: GNU sed sed -r '/title.*java/I!d;s/.*:.(.*).}$/\1/' file java cook book.pdf Java book.pdf 回答2: You can try something like this with

How to delete the lines starting from the 1st line till line before encountering the pattern '[ERROR] -17-12-2015' using sed?

▼魔方 西西 提交于 2021-01-27 11:43:29
问题 I need to delete lines from the 1st line till line before encountering the pattern '[ERROR] -17-12-2015' Currently I am trying the below command but unfortunately it does not find the pattern itself: sed '1,/[ERROR] -17-12-2015/d' errLog What is wrong here? Secondly, the above script will also delete the line containing pattern '[ERROR] -17-12-2015' , is it possible to delete only the lines from the first line to the line before encountering this pattern ? The Sample input is: [ERROR] -09-11

What does this mean in Linux sed '$a\' a.txt

倾然丶 夕夏残阳落幕 提交于 2021-01-27 06:09:30
问题 I have this line in my Linux shell script sed '$a\' < file_a.txt Afraid to remove it from the code and cannot find out what it is for. 回答1: It makes sure the output will end with a newline; see: echo -ne test | sed '$a\' # same output as: echo test | sed '$a\' As you can see with the previous code, a carriage return isn't added in the second example but one is added in the first example. Of course, if you remove the sed part, the output will be different since the first echo statement has no

What does this mean in Linux sed '$a\' a.txt

我与影子孤独终老i 提交于 2021-01-27 06:08:42
问题 I have this line in my Linux shell script sed '$a\' < file_a.txt Afraid to remove it from the code and cannot find out what it is for. 回答1: It makes sure the output will end with a newline; see: echo -ne test | sed '$a\' # same output as: echo test | sed '$a\' As you can see with the previous code, a carriage return isn't added in the second example but one is added in the first example. Of course, if you remove the sed part, the output will be different since the first echo statement has no

Tiebreaker for same-length regex alternatives with same starting position

寵の児 提交于 2021-01-27 05:34:35
问题 Using GNU sed (with the -r flag for clarity), the following two substitutions on the input string ab give the same result: s/(.)(.)|(.)(.)$/\2\1\3\4/ and s/(.)(.)$|(.)(.)/\1\2\4\3/ both give ba . It would appear that the alternative (.)(.) (the one without $ ) succeeds in both substitutions, regardless of whether its position as the first or second alternative. Why is this the case? What is the tie-breaker for such alternatives? The POSIX specification of regular expressions specifies 1 the

How to use sed command to add a string before a pattern string?

放肆的年华 提交于 2021-01-27 02:37:14
问题 I want to use sed to modify my file named "baz". When i search a pattern foo , foo is not at the beginning or end of line, i want to append bar before foo, how can i do it using sed? Input file named baz: blah_foo_blahblahblah blah_foo_blahblahblah blah_foo_blahblahblah blah_foo_blahblahblah Output file blah_barfoo_blahblahblah blah_barfoo_blahblahblah blah_barfoo_blahblahblah blah_barfoo_blahblahblah 回答1: You can just use something like: sed 's/foo/barfoo/g' baz (the g at the end means

How to use sed command to add a string before a pattern string?

点点圈 提交于 2021-01-27 02:35:28
问题 I want to use sed to modify my file named "baz". When i search a pattern foo , foo is not at the beginning or end of line, i want to append bar before foo, how can i do it using sed? Input file named baz: blah_foo_blahblahblah blah_foo_blahblahblah blah_foo_blahblahblah blah_foo_blahblahblah Output file blah_barfoo_blahblahblah blah_barfoo_blahblahblah blah_barfoo_blahblahblah blah_barfoo_blahblahblah 回答1: You can just use something like: sed 's/foo/barfoo/g' baz (the g at the end means

Trying to remove non-printable charaters(junk values) from a UNIX file

蓝咒 提交于 2021-01-20 04:18:50
问题 I am trying to remove non-printable character (for e.g. ^@ ) from records in my file. Since the volume to records is too big in the file using cat is not an option as the loop is taking too much time. I tried using sed -i 's/[^@a-zA-Z 0-9`~!@#$%^&*()_+\[\]\\{}|;'\'':",.\/<>?]//g' FILENAME but still the ^@ characters are not removed. Also I tried using awk '{ sub("[^a-zA-Z0-9\"!@#$%^&*|_\[](){}", ""); print } FILENAME > NEW FILE but it also did not help. Can anybody suggest some alternative

Trying to remove non-printable charaters(junk values) from a UNIX file

一曲冷凌霜 提交于 2021-01-20 04:15:04
问题 I am trying to remove non-printable character (for e.g. ^@ ) from records in my file. Since the volume to records is too big in the file using cat is not an option as the loop is taking too much time. I tried using sed -i 's/[^@a-zA-Z 0-9`~!@#$%^&*()_+\[\]\\{}|;'\'':",.\/<>?]//g' FILENAME but still the ^@ characters are not removed. Also I tried using awk '{ sub("[^a-zA-Z0-9\"!@#$%^&*|_\[](){}", ""); print } FILENAME > NEW FILE but it also did not help. Can anybody suggest some alternative

Sed failed to match non whitespace characters with character class

拟墨画扇 提交于 2021-01-19 06:32:31
问题 I want to extract filter rules configured in /etc/lvm/lvm.conf , like filter = [ "r|/dev/sda|" ] . I want sed to return "r|/dev/sda|" . So I have tried the following script: echo ' filter = [ "r|/dev/sda|" ] ' | sed -r 's:^\s*filter\s*=\s*\[\s*([^\s]+)\s*\]:\1:g' But it didn't work, the script has returned filter = [ "r|/dev/sda|" ] . I've tried a few on line regex tester, the group has been matched correctly. However, if I replace [^\s]+ by .+ , it works. Doesn't [^\s]+ mean more than one