![]() |
Hei og Velkommen fra USA til UNIX og Linux Forums! Takk for besøket og Delta i vårt globale samfunn.
|
|
google unix.com
|
|||||||
| Forums | Registrer | Forum Rules | Lenker | Album | FAQ | Medlemsliste | Kalender | Søke | Dagens innlegg | Marker forumene som lest |
| Shell programmering og Skripting Post spørsmål om ksh, csh, SH, Bash, Perl, PHP, SED, awk og ANDRE shell scripts og Shell skriptespråk her. |
Mer UNIX og Linux Forum Emner Du kan finne nyttig
|
||||
| Tråd | Tråd startet | Forum | Svar | Siste innlegg |
| hvordan du konverterer Fast lengde filen til avgrenset fil. | satyam_sat | Shell programmering og Skripting | 7 | 04-03-2008 02:41 |
| Konvertere en avgrenset fil til fast bredde fil | raghavan.aero | Shell programmering og Skripting | 2 | 06-06-2007 02:44 |
| konvertere XML fil inn Tekstfil (fast lengde) | ram2s2001 | Shell programmering og Skripting | 0 | 11-03-2005 01:28 |
| Konverter avgrenset til fast lengde | nelson553011 | Shell programmering og Skripting | 14 | 10-27-2005 04:04 |
| hvordan kan endre UDP lengde? | Vvlad | IP Networking | 4 | 08-14-2003 07:37 |
![]() |
|
|
LinkBack | Thread Tools | Søk i denne tråden | Rate Thread | Visningsmoduser |
|
|
|
||||
|
Kan vi konvertere en "|"-filen til en fast lengde??
Hei Alle,
Jeg har en pipe skilt flat file.But det ofte er noe problem med records.So er det mulig å konvertere '|' delt fil til en fast lengde fil ved hjelp av et skript. Filen har 11 kolonner som betyr 10 pipes.Your hjelpe er verdsatt. Jeg bruker Sun OS Version 5.10 Takk, Kumar |
|
||||
|
Som "felt" i filen er atskilt med en konstant røye ("|") bruk kutte å skille dem, deretter skrive ut linjene via printf (jeg antar Kornshell her, bruk "ekko" i stedet for "print" hvis du bruker noe annet):
Code:
cat infile | while read line ; do
# split each input line to fields and catch these in variables
field1="$(print - "$line" | cut -d'|' -f1)"
field2="$(print - "$line" | cut -d'|' -f2)"
field3="$(print - "$line" | cut -d'|' -f3)"
.....
# after you are done with the line print it out again
# i assume here that the first column should be 20 chars wide, the next
# two 15, and so on. see the second example below.
printf '%20s %15s %15s [...]\n' "$field1" "field2" "$field3" [...] >> outfile
done
Code:
maxlength1=0
maxlength2=0
....
cat infile | while read line ; do
# in the first run we split and get the max width for each column
field1="$(print - "$line" | cut -d'|' -f1)"
length1=$(print - "$field1" | wc -c)
if [ $length1 -gt $maxlength1 ] ; then
maxlength1=$length1
fi
field2="$(print - "$line" | cut -d'|' -f2)"
length2=$(print - "$field2" | wc -c)
if [ $length2 -gt $maxlength2 ] ; then
maxlength2=$length1
fi
.....
done
# put together the output template for printf
template='%'"$maxlength1"'s %"'$maxlength2"'s [.....]\n'
cat infile | while read line ; do
# in the second run we split again and print using the found widths
field1="$(print - "$line" | cut -d'|' -f1)"
field2="$(print - "$line" | cut -d'|' -f2)"
....
printf "$template" "$field1" "field2" "$field3" [...] >> outfile
done
Bakunin |
![]() |
| Hugseliste |
| Thread Tools | Søk i denne tråden |
| Visningsmoduser | Ranger denne tråden |
|
|