mnmonu
February 15, 2012, 2:04am
1
Hi,
I have a file input.txt.
cat input.txt
output is as follows :
I want the output looking like:
cat output.txt
I have a script.
cat input.txt | tr -d '\n' | sed 's/\("name"\)/\1\n/g' > output.txt
here I have to check "name" string and then enter newline. But I want the output as above. Anyone can help me please. If possible count ~ character equal to 6 then new line.
Thanks in advance
Could this help you ?
tr -d '\n' < Filename | awk -F"~" '{for(i=1;i<=NF;i++){printf i%6?$i"~":$i"~\n"}} END {printf "\n"}'
mnmonu
February 15, 2012, 4:30am
3
Hi pravin,
I want to count "~ this string 6 times.because file data value may be ~ character that is why only ~ count is not working. Count should be "~ character combination.
my code is
cat filename | tr -d '\n' | sed 's/\([^"\~]*"~[^"\~]*"~[^"\~]*"~[^"\~]*"~[^"\~]*"~[^"\~]*"~\)/\1\n/g'
but my code not working properly.
ygemici
February 15, 2012, 5:00am
4
like this
# sed ':e;N;s/\n//g;te' file1 |sed 's/~/~X_XsedX/6;s/~/~X_XsedX/12;s/X_XsedX/\n/g'
"0001"~"name"~"bb"~"20.25"~"0010"~"abc"~
"0002"~"name"~"dd"~"35.50"~"25"~"xxx"~
"0003"~"name"~"aa"~"21.35"~"0056"~"ijk"~
regards
ygemici
mnmonu
February 15, 2012, 5:08am
5
Hi ygemici,
Your code is correct. But when file contain 10000 row then problem. Then some loop is needed.
awk:
awk '{gsub(/[^~]*~"name"/,RS RS "&")}NF' infile | awk '{gsub(ORS,x)}1' RS=
mnmonu
February 15, 2012, 7:01am
7
Hi Scrutinizer,
Your script is running properly when file contain few amount of data. But when file contain huge data that time your script getting lots of time.And also one problem. When I redirect your script then output file only one line.
awk '{gsub(/[^~]*~"name"/,RS RS "&")}NF' input_file | awk '{gsub(ORS,x)}1' RS= > output_file
ygemici
February 15, 2012, 7:16am
8
try this
# cat file1
"0001"~"name"~"bb"~"20.25"~"0010"
~"abc"~"0002"~"name"~"dd"~"35.50"~"25"~
"xxx"~"0003"~"name"~"aa"~"21.3
5"~"0056"~"ijk"~
"0001"~"name"~"bb"~"20.25"~"0010"
~"abc"~"0002"~"name"~"dd"~"35.50"~"25"~
"xxx"~"0003"~"name"~"aa"~"21.3
5"~"0056"~"ijk"~
"0001"~"name"~"bb"~"20.25"~"0010"
"0001"~"name"~"bb"~"20.25"~"0010"
~"abc"~"0002"~"name"~"dd"~"35.50"~"25"~
"xxx"~"0003"~"name"~"aa"~"21.3
5"~"0056"~"ijk"~
~"abc"~"0002"~"name"~"dd"~"35.50"~"25"~
"xxx"~"0003"~"name"~"aa"~"21.3
5"~"0056"~"ijk"~
# ./justdoit file1 "~" 6
"0001"~"name"~"bb"~"20.25"~"0010"~"abc"~
"0002"~"name"~"dd"~"35.50"~"25"~"xxx"~
"0003"~"name"~"aa"~"21.35"~"0056"~"ijk"~
"0001"~"name"~"bb"~"20.25"~"0010"~"abc"~
"0002"~"name"~"dd"~"35.50"~"25"~"xxx"~
"0003"~"name"~"aa"~"21.35"~"0056"~"ijk"~
"0001"~"name"~"bb"~"20.25"~"0010""0001"~"name"~
"bb"~"20.25"~"0010"~"abc"~"0002"~"name"~
"dd"~"35.50"~"25"~"xxx"~"0003"~"name"~
"aa"~"21.35"~"0056"~"ijk"~~"abc"~
"0002"~"name"~"dd"~"35.50"~"25"~"xxx"~
"0003"~"name"~"aa"~"21.35"~"0056"~"ijk"~
for save a file
# ./justdoit file1 "~" 6 >output
# cat justdoit
#!/bin/bash
## justdoit @ygemici unix.com simple parser ##
## Usage e.g -> ./justdoit FILENAME "SPLITCHAR" COUNT ##
backup() {
cp $1 $1.bck && cp $1.bck $1.sed
}
backup $1
if [ ! $? -eq 0 ] ; then
echo "There is a problem while backuping your FILE!!";exit 1
fi
doit() {
len=$(tr -cd "$2" <$1|wc -c)
sed ':e;N;s/\n//g;te' ${1}.sed >${1}.sed.bck && mv ${1}.sed.bck ${1}.sed
for ((i=$3;i<${len};i=i+$3));
do
sed 's/~/~X_XsedX/'$i'' ${1}.sed >${1}.sed.bck && mv ${1}.sed.bck ${1}.sed
done
sed 's/X_XsedX/\n/g' ${1}.sed
}
doit "$1" "$2" $3;rm -f ${1}.sed
regards,
ygemici
1 Like
Another one:
awk 1 RS=\~ infile | awk 'NF{if (/name/)print RS;print p;p=$0}END{print p}' | awk '{gsub(ORS,"~");print $0 "~"}' RS=
1 Like
mnmonu
February 16, 2012, 12:27am
10
Thanks Scrutinizer.
But little problem in your script. End of output file, print the line no. Just like
see last line last value print "0500", that is extra
Please help me
I can't reproduced this, could you tail your input sample and post it?